Class: Aspire::Enumerator::JSONEnumerator
- Inherits:
-
Object
- Object
- Aspire::Enumerator::JSONEnumerator
- Defined in:
- lib/aspire/enumerator/json_enumerator.rb
Overview
Enumerates over the properties of a JSON data structure
Direct Known Subclasses
Instance Attribute Summary collapse
-
#hooks ⇒ Hash
The callback hooks.
-
#yielder ⇒ Enumerator::Yielder
The yielder instance from an Enumerator.
Instance Method Summary collapse
- #[](hook, *args, **kwargs) ⇒ Object
- #[]=(hook, proc) ⇒ Object
-
#array(key, array, index) ⇒ void
Enumerates an array of JSON data structures.
-
#enumerate(key, value, index = nil) ⇒ void
Enumerates the property/value pairs of a JSON data structure.
-
#enumerator(key, value) ⇒ Enumerator
Returns an enumerator enumerating property/value pairs of JSON data.
-
#hash(key, hash, index = nil) ⇒ void
Enumerates the property/value pairs of a JSON hash.
-
#initialize(yielder = nil, **hooks) ⇒ void
constructor
Initialises a new JSONEnumerator instance.
-
#property(key, value, index = nil) ⇒ void
Yields a property/value pair.
Constructor Details
#initialize(yielder = nil, **hooks) ⇒ void
Initialises a new JSONEnumerator instance
42 43 44 45 |
# File 'lib/aspire/enumerator/json_enumerator.rb', line 42 def initialize(yielder = nil, **hooks) self.hooks = hooks self.yielder = yielder end |
Instance Attribute Details
#hooks ⇒ Hash
Returns the callback hooks.
8 9 10 |
# File 'lib/aspire/enumerator/json_enumerator.rb', line 8 def hooks @hooks end |
#yielder ⇒ Enumerator::Yielder
Returns the yielder instance from an Enumerator.
13 14 15 |
# File 'lib/aspire/enumerator/json_enumerator.rb', line 13 def yielder @yielder end |
Instance Method Details
#[](hook, *args, **kwargs) ⇒ Object
47 48 49 50 51 |
# File 'lib/aspire/enumerator/json_enumerator.rb', line 47 def [](hook, *args, **kwargs) h = hooks[hook] return true unless h && h.respond_to?(:call) h.call(*args, **kwargs) ? true : false end |
#[]=(hook, proc) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/aspire/enumerator/json_enumerator.rb', line 53 def []=(hook, proc) unless proc.is_a?(Proc) || proc.is_a?(Method) raise ArgumentError, 'Proc or Method expected' end hooks[hook] = proc end |
#array(key, array, index) ⇒ void
This method returns an undefined value.
Enumerates an array of JSON data structures
66 67 68 69 70 71 72 73 74 |
# File 'lib/aspire/enumerator/json_enumerator.rb', line 66 def array(key, array, index) return unless self[:before_array, key, array, index] i = 0 array.each do |value| enumerate(key, value, i) i += 1 end self[:after_array, key, array, index] end |
#enumerate(key, value, index = nil) ⇒ void
This method returns an undefined value.
Enumerates the property/value pairs of a JSON data structure
82 83 84 85 86 87 88 89 90 |
# File 'lib/aspire/enumerator/json_enumerator.rb', line 82 def enumerate(key, value, index = nil) if value.is_a?(Array) array(key, value, index) elsif value.is_a?(Hash) hash(key, value, index) else property(key, value, index) end end |
#enumerator(key, value) ⇒ Enumerator
Returns an enumerator enumerating property/value pairs of JSON data
96 97 98 99 100 101 |
# File 'lib/aspire/enumerator/json_enumerator.rb', line 96 def enumerator(key, value) ::Enumerator.new do |yielder| self.yielder = yielder enumerate(key, value) end end |
#hash(key, hash, index = nil) ⇒ void
This method returns an undefined value.
Enumerates the property/value pairs of a JSON hash
109 110 111 112 113 114 115 |
# File 'lib/aspire/enumerator/json_enumerator.rb', line 109 def hash(key, hash, index = nil) return unless self[:before_hash, key, hash, index] hash.each do |k, v| v.is_a?(Array) || v.is_a?(Hash) ? enumerate(k, v) : property(k, v) end self[:after_hash, key, hash, index] end |
#property(key, value, index = nil) ⇒ void
This method returns an undefined value.
Yields a property/value pair
123 124 125 126 127 |
# File 'lib/aspire/enumerator/json_enumerator.rb', line 123 def property(key, value, index = nil) return unless self[:before_yield, key, value, index] yielder << [key, value, index] self[:after_yield, hooks, key, value, index] end |