Class: Mixture::Attribute

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

Overview

An attribute for a mixture object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, list, options = {}) ⇒ Attribute

Initialize the attribute.

Parameters:

  • name (Symbol)

    The name of the attribute.

  • list (AttributeList)

    The attribute list this attribute is a part of.

  • options (Hash) (defaults to: {})

    The optiosn for the attribute.



24
25
26
27
28
# File 'lib/mixture/attribute.rb', line 24

def initialize(name, list, options = {})
  @name = name
  @list = list
  @options = options
end

Instance Attribute Details

#getterSymbol (readonly)

The getter method for this attribute.

Examples:

Retrieve the getter.

attribute.getter # => :name

Returns:

  • (Symbol)


56
57
58
# File 'lib/mixture/attribute.rb', line 56

def getter
  @name
end

#ivarSymbol (readonly)

The instance variable for this attribute.

Examples:

Retrieve the instance variable.

attribute.ivar # => :@name

Returns:

  • (Symbol)


46
47
48
# File 'lib/mixture/attribute.rb', line 46

def ivar
  @_ivar ||= :"@#{@name}"
end

#nameSymbol (readonly)

The name of the attribute.

Returns:

  • (Symbol)


10
11
12
# File 'lib/mixture/attribute.rb', line 10

def name
  @name
end

#optionsHash (readonly)

The options for the attribute. This is mainly used for coercion and validation.

Returns:

  • (Hash)


16
17
18
# File 'lib/mixture/attribute.rb', line 16

def options
  @options
end

#setterSymbol (readonly)

The setter method for this attribute.

Examples:

Retrieve the setter.

attribute.setter # :name=

Returns:

  • (Symbol)


66
67
68
# File 'lib/mixture/attribute.rb', line 66

def setter
  @_setter ||= :"#{@name}="
end

Instance Method Details

#update(value) ⇒ Object

Update the attribute with the given value. It runs the value through the callbacks, and returns a new value given by the callbacks.

Parameters:

  • value (Object)

    The new value.

Returns:

  • (Object)

    The new new value.



36
37
38
# File 'lib/mixture/attribute.rb', line 36

def update(value)
  @list.callbacks[:update].inject(value) { |a, e| e.call(self, a) }
end