Class: Humanoid::Field

Inherits:
Object show all
Defined in:
lib/humanoid/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create the new field with a name and optional additional options. Valid options are :default

Options:

name: The name of the field as a Symbol. options: A Hash of options for the field.

Example:

Field.new(:score, :default => 0)



21
22
23
24
25
# File 'lib/humanoid/field.rb', line 21

def initialize(name, options = {})
  @name = name
  @default = options[:default]
  @type = options[:type] || String
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



5
6
7
# File 'lib/humanoid/field.rb', line 5

def default
  @default
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/humanoid/field.rb', line 5

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/humanoid/field.rb', line 5

def type
  @type
end

Instance Method Details

#get(object) ⇒ Object

Used for retrieving the object out of the attributes hash.



34
35
36
# File 'lib/humanoid/field.rb', line 34

def get(object)
  type.get(object)
end

#set(object) ⇒ Object

Used for setting an object in the attributes hash. If nil is provided the default will get returned if it exists.



29
30
31
# File 'lib/humanoid/field.rb', line 29

def set(object)
  object.nil? ? default : type.set(object)
end