Class: Hoodoo::Presenters::Array
- Includes:
- BaseDSL
- Defined in:
- lib/hoodoo/presenters/types/array.rb
Overview
A JSON Array schema member.
Instance Attribute Summary collapse
-
#properties ⇒ Object
The properties of this object, an
array
ofField
instances.
Attributes inherited from Field
Instance Method Summary collapse
-
#render(data, target) ⇒ Object
Render an array into the target hash based on the internal state that describes this instance’s current path (position in the heirarchy of nested schema entities).
-
#validate(data, path = '') ⇒ Object
Check if data is a valid Array and return a Hoodoo::Errors instance.
-
#walk(&block) ⇒ Object
Invoke a given block, passing this item; call recursively for any defined sub-fields too.
Methods included from BaseDSL
#array, #boolean, #date, #datetime, #decimal, #enum, #float, #hash, #integer, #internationalised, #is_internationalised?, #object, #resource, #string, #tags, #text, #type, #uuid
Methods inherited from Field
#full_path, #has_default?, #initialize
Constructor Details
This class inherits a constructor from Hoodoo::Presenters::Field
Instance Attribute Details
#properties ⇒ Object
The properties of this object, an array
of Field
instances.
12 13 14 |
# File 'lib/hoodoo/presenters/types/array.rb', line 12 def properties @properties end |
Instance Method Details
#render(data, target) ⇒ Object
Render an array into the target hash based on the internal state that describes this instance’s current path (position in the heirarchy of nested schema entities).
data
-
The Array to render.
target
-
The Hash that we render into. A “path” of keys leading to nested Hashes is built via super(), with the final key entry yielding the rendered array.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/hoodoo/presenters/types/array.rb', line 51 def render( data, target ) # Data provided is explicitly nil or not an array? Don't need to render # anything beyond 'nil' at the field (the not-array case covers nil and # covers invalid input, which is treated as nil). return super( nil, target ) if ! data.is_a?( ::Array ) # Otherwise, start looking at rendering array contents (even if the # input array is empty). This relies on pass-by-reference; we'll update # this specific instance of 'array' later. Call 'super' to render the # 'array' instance in place in 'target' straight away... array = [] path = super( array, target ) # ...then look at rendering the input entries of 'data' into 'array'. if @properties.nil? # Must modify existing instance of 'array', so use 'push()' array.push( *data ) else data.each do | item | # We have properties defined so array values (in "item") must be # Hashes. If non-Hash, treat as if nil; explicit-nil-means-nil. unless item.is_a?( ::Hash ) # Must modify existing instance of 'array', so use 'push()' array.push( nil ) next end subtarget = {} @properties.each do | name, property | name = name.to_s has_key = item.has_key?( name ) next unless has_key || property.has_default?() property.render( has_key ? item[ name ] : property.default, subtarget ) end rendered = subtarget.empty? ? {} : read_at_path( subtarget, path ) # Must modify existing instance of 'array', so use 'push()' array.push( rendered ) end end end |
#validate(data, path = '') ⇒ Object
Check if data is a valid Array and return a Hoodoo::Errors instance.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/hoodoo/presenters/types/array.rb', line 16 def validate( data, path = '' ) errors = super( data, path ) return errors if errors.has_errors? || ( ! @required && data.nil? ) if data.is_a?( ::Array ) # No array entry schema? No array entry validation, then. unless @properties.nil? data.each_with_index do | item, index | @properties.each do | name, property | rdata = ( item.is_a?( ::Hash ) && item.has_key?( name ) ) ? item[ name ] : nil indexed_path = "#{ full_path( path ) }[#{ index }]" errors.merge!( property.validate( rdata, indexed_path ) ) end end end else errors.add_error( 'generic.invalid_array', :message => "Field `#{ full_path( path ) }` is an invalid array", :reference => { :field_name => full_path( path ) } ) end errors end |
#walk(&block) ⇒ Object
Invoke a given block, passing this item; call recursively for any defined sub-fields too. See Hoodoo::Presenters::Base#walk for why.
- &block
-
Mandatory block, which is passed ‘self’ when called.
109 110 111 112 113 114 115 |
# File 'lib/hoodoo/presenters/types/array.rb', line 109 def walk( &block ) block.call( self ) @properties.each do | name, property | property.walk( &block ) end unless @properties.nil? end |