Class: Hoodoo::Presenters::Field

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Field

Initialize a Field instance with the appropriate name and options.

name

The JSON key.

options

A Hash of 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, 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

Instance Attribute Details

#defaultObject

Default value, if supplied.



16
17
18
# File 'lib/hoodoo/presenters/types/field.rb', line 16

def default
  @default
end

#nameObject

The name of the field.



8
9
10
# File 'lib/hoodoo/presenters/types/field.rb', line 8

def name
  @name
end

#requiredObject

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

path

The 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.

Returns:



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.

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).



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