Class: Tataru::Flattener
- Inherits:
-
Object
- Object
- Tataru::Flattener
- Defined in:
- lib/tataru/flattener.rb
Overview
flattens properties to make them digestable
Instance Method Summary collapse
- #flatten(value, name) ⇒ Object
- #flatten_array(value, name) ⇒ Object
- #flatten_hash(value, name) ⇒ Object
- #flatten_literal(value, name) ⇒ Object
- #flatten_output(value, name) ⇒ Object
- #flatten_resource(value, name) ⇒ Object
- #flattened ⇒ Object
-
#initialize(value) ⇒ Flattener
constructor
A new instance of Flattener.
Constructor Details
#initialize(value) ⇒ Flattener
Returns a new instance of Flattener.
6 7 8 9 |
# File 'lib/tataru/flattener.rb', line 6 def initialize(value) @value = value @result = {} end |
Instance Method Details
#flatten(value, name) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/tataru/flattener.rb', line 16 def flatten(value, name) type = value.class.name .sub(/^Tataru::Representations::/, '') .sub(/Representation$/, '') .downcase method_name = :"flatten_#{type}" raise "cannot flatten #{value.inspect}" unless respond_to?(method_name) send(method_name, value, name) end |
#flatten_array(value, name) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/tataru/flattener.rb', line 34 def flatten_array(value, name) refs = {} value.value.each_with_index do |val, i| key = :"#{name}_#{i}" flatten(val, key) refs[i] = key end @result[name] = { type: :array, references: refs } end |
#flatten_hash(value, name) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/tataru/flattener.rb', line 47 def flatten_hash(value, name) refs = {} value.value.each do |k, v| key = :"#{name}_#{k}" flatten(v, key) refs[k] = key end @result[name] = { type: :hash, references: refs } end |
#flatten_literal(value, name) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/tataru/flattener.rb', line 27 def flatten_literal(value, name) @result[name] = { type: :literal, value: value.value } end |
#flatten_output(value, name) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/tataru/flattener.rb', line 73 def flatten_output(value, name) @result[name] = { type: :output, resource: value.resource_name, output: value.output_field_name } end |
#flatten_resource(value, name) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/tataru/flattener.rb', line 60 def flatten_resource(value, name) refs = {} value.properties.each do |k, v| key = :"#{name}_#{k}" flatten(v, key) refs[k] = key end @result[name] = { type: :hash, references: refs } end |
#flattened ⇒ Object
11 12 13 14 |
# File 'lib/tataru/flattener.rb', line 11 def flattened flatten(@value, :top) @result end |