Class: Hashme::Property

Inherits:
Object
  • Object
show all
Defined in:
lib/hashme/property.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, opts = {}) ⇒ Property

Returns a new instance of Property.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/hashme/property.rb', line 6

def initialize(name, type, opts = {})
  @name = name.to_sym

  # Always set type to base type
  if type.is_a?(Array) && !type.first.nil?
    @array = true
    @type = type.first
  else
    @array = false
    @type = type
  end

  # Handle options
  @default = opts[:default]
end

Instance Attribute Details

#arrayObject (readonly)

Returns the value of attribute array.



4
5
6
# File 'lib/hashme/property.rb', line 4

def array
  @array
end

#defaultObject (readonly)

Returns the value of attribute default.



4
5
6
# File 'lib/hashme/property.rb', line 4

def default
  @default
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/hashme/property.rb', line 4

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/hashme/property.rb', line 4

def type
  @type
end

Instance Method Details

#build(owner, value) ⇒ Object

Build a new object of the type defined by the property.



31
32
33
34
35
36
37
# File 'lib/hashme/property.rb', line 31

def build(owner, value)
  if array && value.is_a?(Array)
    CastedArray.new(self, owner, value)
  else
    PropertyCasting.cast(self, owner, value)
  end
end

#to_sObject



22
23
24
# File 'lib/hashme/property.rb', line 22

def to_s
  name.to_s
end

#to_symObject



26
27
28
# File 'lib/hashme/property.rb', line 26

def to_sym
  name
end