Skip to content

Commit 2372942

Browse files
committed
Add a JSON addition for Enumerator::ArithmeticSequence
1 parent 4f876a8 commit 2372942

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

lib/json.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@
408408
# - Complex: <tt>require 'json/add/complex'</tt>
409409
# - Date: <tt>require 'json/add/date'</tt>
410410
# - DateTime: <tt>require 'json/add/date_time'</tt>
411+
# - Enumerator::ArithmeticSequence: <tt>require 'json/add/arithmetic_sequence'</tt>
411412
# - Exception: <tt>require 'json/add/exception'</tt>
412413
# - OpenStruct: <tt>require 'json/add/ostruct'</tt>
413414
# - Range: <tt>require 'json/add/range'</tt>

lib/json/add/arithmetic_sequence.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
require 'json' unless defined?(JSON::JSON_LOADED) && JSON::JSON_LOADED
4+
5+
class Enumerator
6+
class ArithmeticSequence
7+
# See #as_json.
8+
def self.json_create(object)
9+
Range.new(*object.values_at('b', 'e', 'x')) % object['s']
10+
end
11+
12+
# Methods <tt>Enumerator::ArithmeticSequence#as_json</tt> and
13+
# +Enumerator::ArithmeticSequence.json_create+ can be used to serialize and
14+
# deserialize an \ArithmeticSequence object. See Marshal[rdoc-ref:Marshal].
15+
#
16+
# \Method <tt>Enumerator::ArithmeticSequence#as_json</tt> serializes +self+,
17+
# returning a 5-element hash representing +self+:
18+
#
19+
# require 'json/add/arithmetic_sequence'
20+
#
21+
# x = 42.step(by: 3, to: 72).as_json
22+
# # => {"json_class"=>"Enumerator::ArithmeticSequence", "b"=>42, "e"=>72, "x"=>false, "s"=>3}
23+
#
24+
# y = ((42...72) % 4).as_json
25+
# # => {"json_class"=>"Enumerator::ArithmeticSequence", "b"=>42, "e"=>72, "x"=>true, "s"=>4}
26+
#
27+
# \Method +JSON.create+ deserializes such a hash, returning an
28+
# \ArithmeticSequence object:
29+
#
30+
# Enumerator::ArithmeticSequence.json_create(x) # => ((42..72).%(3))
31+
# Enumerator::ArithmeticSequence.json_create(y) # => ((42...72).%(4))
32+
#
33+
def as_json(*)
34+
{
35+
JSON.create_id => self.class.name,
36+
'b' => self.begin,
37+
'e' => self.end,
38+
'x' => exclude_end?,
39+
's' => step
40+
}
41+
end
42+
43+
# Returns a JSON string representing +self+:
44+
#
45+
# require 'json/add/arithmetic_sequence'
46+
#
47+
# puts 42.step(by: 3, to: 72).to_json
48+
# puts ((42...72) % 4).to_json
49+
#
50+
# Output:
51+
#
52+
# {"json_class":"Enumerator::ArithmeticSequence","b":42,"e":72,"x":false,"s":3}
53+
# {"json_class":"Enumerator::ArithmeticSequence","b":42,"e":72,"x":true,"s":4}
54+
#
55+
def to_json(*) = as_json.to_json(*)
56+
end
57+
end

tests/json_addition_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#frozen_string_literal: false
22
require_relative 'test_helper'
3+
require 'json/add/arithmetic_sequence'
34
require 'json/add/core'
45
require 'json/add/complex'
56
require 'json/add/rational'
@@ -196,4 +197,11 @@ def test_set
196197
s = Set.new([:a, :b, :c, :a])
197198
assert_equal s, JSON.parse(JSON(s), :create_additions => true)
198199
end
200+
201+
def test_arithmetic_sequence
202+
include_end = 42.step(by: 3, to: 72)
203+
assert_equal include_end, JSON.parse(JSON(include_end), :create_additions => true)
204+
exclude_end = ((42...72) % 4)
205+
assert_equal exclude_end, JSON.parse(JSON(exclude_end), :create_additions => true)
206+
end
199207
end

0 commit comments

Comments
 (0)