Class: Aspire::Enumerator::JSONEnumerator

Inherits:
Object
  • Object
show all
Defined in:
lib/aspire/enumerator/json_enumerator.rb

Overview

Enumerates over the properties of a JSON data structure

Direct Known Subclasses

LinkedDataURIEnumerator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yielder = nil, **hooks) ⇒ void

Initialises a new JSONEnumerator instance

Parameters:

  • yielder (Enumerator::Yielder) (defaults to: nil)

    the yielder from an Enumerator

  • hooks (Hash)

    a hash of executable callback hooks: {

    after_array: proc { |key,value,index| }
    after_hash: proc { |key,value,index| }
    after_yield: proc { |key,value,index| }
    before_array: proc { |key,value,index| }
    before_hash: proc { |key,value,index| }
    before_yield: proc { |key,value,index| }
    

    }

    Each callback is a Proc accepting a property key (name), value, and optionally the numeric index of the property in its parent array (this is nil if the property is not an array member).

    Value is an array for after/before_array, a hash for after/before_hash and any type for after/before_yield.

    All before hooks must return a truthy value to allow processing of the value, or a falsey value to prevent processing of the value.

    Filters should be implemented in before hooks

    Before hooks can also be used to process arrays and hashes as a whole. They should return false if property-level processing is not required.



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

#hooksHash

Returns the callback hooks.

Returns:

  • (Hash)

    the callback hooks



8
9
10
# File 'lib/aspire/enumerator/json_enumerator.rb', line 8

def hooks
  @hooks
end

#yielderEnumerator::Yielder

Returns the yielder instance from an Enumerator.

Returns:

  • (Enumerator::Yielder)

    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

Parameters:

  • key (String)

    the property name

  • array (Object)

    the property value

  • index (Integer)

    the index of the property in its parent array, or nil if not part of an array



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

Parameters:

  • key (String)

    the property name

  • value (Object)

    the property value

  • index (Integer) (defaults to: nil)

    the index of the property in its parent array, or nil if not part of an array



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

Parameters:

  • key (String)

    the initial key of the data

  • value (Object)

    the initial value of the data

Returns:



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

Parameters:

  • key (String)

    the property name

  • hash (Hash)

    the hash to enumerate

  • index (Integer) (defaults to: nil)

    the index of the property in its parent array, or nil if not part of an array



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

Parameters:

  • key (String)

    the property name

  • value (Object)

    the property value

  • index (Integer) (defaults to: nil)

    the index of the property in its parent array, or nil if not part of an array



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