Class: Hoodoo::Presenters::Field
- Inherits:
-
Object
- Object
- Hoodoo::Presenters::Field
- Defined in:
- lib/hoodoo/presenters/types/field.rb
Overview
A JSON schema member
Direct Known Subclasses
Array, Boolean, Date, DateTime, Decimal, Enum, Float, Hash, Integer, Object, String, Text, UUID
Instance Attribute Summary collapse
-
#default ⇒ Object
Default value, if supplied.
-
#name ⇒ Object
The name of the field.
-
#required ⇒ Object
trueif the field is required.
Instance Method Summary collapse
-
#full_path(path) ⇒ Object
- Return the full path and name of this field
path The JSON path or nil, e.g.
- Return the full path and name of this field
-
#has_default? ⇒ Boolean
Does this property have a defined default (which may be defined as
nil) rather than having no defined value (+nil+ or otherwise)? Returnstrueif it has a default,falseif it has no default. -
#initialize(name, options = {}) ⇒ Field
constructor
Initialize a Field instance with the appropriate name and options.
-
#render(data, target) ⇒ Object
Dive down into a given hash along path array
@path, building new hash entries if necessary at each path level until the last one. -
#validate(data, path = '') ⇒ Object
Check if data is required and return a Hoodoo::Errors instance.
-
#walk(&block) ⇒ Object
Invoke a given block, passing this item.
Constructor Details
#initialize(name, options = {}) ⇒ Field
Initialize a Field instance with the appropriate name and options.
nameThe JSON key.
optionsA
Hashof options, e.g. :required => true.
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/hoodoo/presenters/types/field.rb', line 23 def initialize(name, = {}) @name = name.to_s @required = .has_key?( :required ) ? [ :required ] : false @path = .has_key?( :path ) ? [ :path ] : [] if .has_key?( :default ) @has_default = true @default = Hoodoo::Utilities.stringify( [ :default ] ) else @has_default = false @default = nil end end |
Instance Attribute Details
#default ⇒ Object
Default value, if supplied.
16 17 18 |
# File 'lib/hoodoo/presenters/types/field.rb', line 16 def default @default end |
#name ⇒ Object
The name of the field.
8 9 10 |
# File 'lib/hoodoo/presenters/types/field.rb', line 8 def name @name end |
#required ⇒ Object
true if the field is required.
12 13 14 |
# File 'lib/hoodoo/presenters/types/field.rb', line 12 def required @required end |
Instance Method Details
#full_path(path) ⇒ Object
Return the full path and name of this field
pathThe JSON path or nil, e.g. 'one.two'
102 103 104 105 106 |
# 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 |
#has_default? ⇒ Boolean
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.
41 42 43 |
# File 'lib/hoodoo/presenters/types/field.rb', line 41 def has_default? !! @has_default end |
#render(data, target) ⇒ Object
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.
dataThe object to build at the final path entry - usually an empty Array or Hash.
targetThe 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).
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# 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 |
#validate(data, path = '') ⇒ Object
Check if data is required and return a Hoodoo::Errors instance.
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# 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 |
#walk(&block) ⇒ Object
Invoke a given block, passing this item. See Hoodoo::Presenters::Base#walk for why.
- &block
Mandatory block, which is passed 'self' when called.
95 96 97 |
# File 'lib/hoodoo/presenters/types/field.rb', line 95 def walk( &block ) block.call( self ) end |