Class: JsonAccessor
- Inherits:
-
Object
- Object
- JsonAccessor
- Defined in:
- lib/random_api/json_accessor.rb
Instance Attribute Summary collapse
-
#access_methods ⇒ Object
Returns the value of attribute access_methods.
-
#code ⇒ Object
Returns the value of attribute code.
-
#mutator ⇒ Object
Returns the value of attribute mutator.
Instance Method Summary collapse
- #build ⇒ Object
- #call(json) ⇒ Object
- #gen_access_key_lambda(key) ⇒ Object
- #gen_root_access_lambda ⇒ Object
-
#initialize(code, mutator) ⇒ JsonAccessor
constructor
A new instance of JsonAccessor.
- #mutate(value) ⇒ Object
Constructor Details
#initialize(code, mutator) ⇒ JsonAccessor
Returns a new instance of JsonAccessor.
4 5 6 7 8 9 |
# File 'lib/random_api/json_accessor.rb', line 4 def initialize(code, mutator) self.code = code self.access_methods = [] self.mutator = mutator build end |
Instance Attribute Details
#access_methods ⇒ Object
Returns the value of attribute access_methods.
2 3 4 |
# File 'lib/random_api/json_accessor.rb', line 2 def access_methods @access_methods end |
#code ⇒ Object
Returns the value of attribute code.
2 3 4 |
# File 'lib/random_api/json_accessor.rb', line 2 def code @code end |
#mutator ⇒ Object
Returns the value of attribute mutator.
2 3 4 |
# File 'lib/random_api/json_accessor.rb', line 2 def mutator @mutator end |
Instance Method Details
#build ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/random_api/json_accessor.rb', line 19 def build keys = code.split(".") if keys.first == "$" access_methods << gen_root_access_lambda keys.shift end keys.each do |key| access_methods << gen_access_key_lambda(key) end end |
#call(json) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/random_api/json_accessor.rb', line 30 def call(json) ret = json access_methods.each do |method| ret = method.(ret) end mutate(ret) end |
#gen_access_key_lambda(key) ⇒ Object
15 16 17 |
# File 'lib/random_api/json_accessor.rb', line 15 def gen_access_key_lambda(key) ->(json) { json[key] } end |
#gen_root_access_lambda ⇒ Object
11 12 13 |
# File 'lib/random_api/json_accessor.rb', line 11 def gen_root_access_lambda ->(json) { json } end |
#mutate(value) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/random_api/json_accessor.rb', line 38 def mutate(value) unless mutator.nil? mutator.(value) else value end end |