Class: CurlyMustache::Attributes::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/curly_mustache/attributes/definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, options = {}) ⇒ Definition

Returns a new instance of Definition.



6
7
8
9
10
11
12
# File 'lib/curly_mustache/attributes/definition.rb', line 6

def initialize(name, type, options = {})
  @options = options.symbolize_keys.reverse_merge :default => nil,
                                                  :allow_nil => true
  @name = name.to_sym
  @type = type.to_sym
  @caster = Types[type].caster
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/curly_mustache/attributes/definition.rb', line 4

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/curly_mustache/attributes/definition.rb', line 4

def type
  @type
end

Instance Method Details

#cast(value) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/curly_mustache/attributes/definition.rb', line 14

def cast(value)
  if value.nil? and @options[:allow_nil]
    nil
  else
    @caster.call(value)
  end
end