Class: Sphene::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/sphene/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, model, type: Types::Default, default: nil) ⇒ Attribute

Returns a new instance of Attribute.



7
8
9
10
11
12
# File 'lib/sphene/attribute.rb', line 7

def initialize(name, model, type: Types::Default, default: nil)
  @name = name
  @model = model
  @type = type
  @default = default
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



5
6
7
# File 'lib/sphene/attribute.rb', line 5

def default
  @default
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/sphene/attribute.rb', line 5

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/sphene/attribute.rb', line 5

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/sphene/attribute.rb', line 5

def type
  @type
end

Instance Method Details

#valueObject



14
15
16
17
18
# File 'lib/sphene/attribute.rb', line 14

def value
  return default_value unless ivar_defined?(:@value_before_cast)
  return @value if ivar_defined?(:@value)
  @value = type.cast(@value_before_cast)
end

#value=(value) ⇒ Object



20
21
22
23
# File 'lib/sphene/attribute.rb', line 20

def value=(value)
  remove_ivar(:@value) if ivar_defined?(:@value)
  @value_before_cast = value
end