A JSON schema member
- F
- H
- N
- R
- V
- W
[RW] | default | Default value, if supplied. |
[RW] | name | The name of the field. |
[RW] | required |
|
Initialize a Field instance with the appropriate name and options.
name
-
The JSON key.
options
-
A
Hash
of options, e.g. :required => true.
Source: show
# File lib/hoodoo/presenters/types/field.rb, line 23 def initialize(name, options = {}) @name = name.to_s @required = options.has_key?( :required ) ? options[ :required ] : false @path = options.has_key?( :path ) ? options[ :path ] : [] if options.has_key?( :default ) @has_default = true @default = Hoodoo::Utilities.stringify( options[ :default ] ) else @has_default = false @default = nil end end
Return the full path and name of this field
path
-
The JSON path or nil, e.g. 'one.two'
Source: show
# File lib/hoodoo/presenters/types/field.rb, line 102 def full_path( path ) return @name.to_s if path.nil? or path.empty? return path.to_s if @name.nil? or @name.empty? path + '.' + @name.to_s end
Does this property have a defined default (which may be defined as
nil
) rather than having no defined value (nil
or
otherwise)? Returns true
if it has a default,
false
if it has no default.
Source: show
# File lib/hoodoo/presenters/types/field.rb, line 41 def has_default? !! @has_default end
Dive down into a given hash along path array +@path+, building new hash entries if necessary at each path level until the last one. At that last level, assign the given object.
data
-
The object to build at the final path entry - usually an empty Array or Hash.
target
-
The Hash (may be initially empty) in which to build the path of keys from internal data +@path+.
Returns the full path array that was used (a clone of +@path+).
Source: show
# File lib/hoodoo/presenters/types/field.rb, line 73 def render( data, target ) return if @name.nil? == false && @name.empty? root = target path = @path.clone final = path.pop.to_s path.each do | element | element = element.to_s root[ element ] = {} unless root.has_key?( element ) root = root[ element ] end root[ final ] = data return path << final end
Check if data is required and return a Hoodoo::Errors instance.
Source: show
# File lib/hoodoo/presenters/types/field.rb, line 47 def validate( data, path = '' ) errors = Hoodoo::Errors.new if data.nil? && @required errors.add_error( 'generic.required_field_missing', :message => "Field `#{ full_path( path ) }` is required", :reference => { :field_name => full_path( path ) } ) end errors end
Invoke a given block, passing this item. See Hoodoo::Presenters::Base#walk for why.
- &block
-
Mandatory block, which is passed 'self' when called.
Source: show
# File lib/hoodoo/presenters/types/field.rb, line 95 def walk( &block ) block.call( self ) end
Dive down into a given target data hash using the given array of path keys, returning the result at the final key in the path. E.g. if the Hash is “{ :foo => { :bar => { :baz => ”hello“ } } }” then a path of “[ :foo, :bar ]” would yield “{ :baz => ”hello“ }”.
Source: show
# File lib/hoodoo/presenters/types/field.rb, line 115 def read_at_path( from_target, with_path ) with_path.each do | element | element = element.to_s from_target = from_target[ element ] break if from_target.nil? end return from_target end
Rename a property to the given name. The internal name is changed and the last path entry set to the same name (if a path is present). Paths of sub-properties (if any) are updated with the parent's new name.
This is a specialist interface which is intended for internal use under unusual circumstances.
name
-
New property name. Must be a String.
Source: show
# File lib/hoodoo/presenters/types/field.rb, line 134 def rename( name ) depth = @path.count - 1 @name = name rewrite_path( depth, name ) end
Change the +@path+ array by writing a given value in at a given index. If this property has any sub-properties, then those are recursively updated to change the same depth item to the new name in all of them.
depth
-
Index into +@path+ to make modifications.
name
-
Value to write at that index.
Source: show
# File lib/hoodoo/presenters/types/field.rb, line 148 def rewrite_path( depth, name ) @path[ depth ] = name if depth >= 0 return if @properties.nil? @properties.each do | property_name, property | property.rewrite_path( depth, name ) end end