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.


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

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)

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

def getter
  @name
end

#ivarSymbol (readonly)

The instance variable for this attribute.

Examples:

Retrieve the instance variable.

attribute.ivar # => :@name

Returns:

  • (Symbol)

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

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

#nameSymbol (readonly)

The name of the attribute.

Returns:

  • (Symbol)

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

def name
  @name
end

#optionsHash (readonly)

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

Returns:

  • (Hash)

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

def options
  @options
end

#setterSymbol (readonly)

The setter method for this attribute.

Examples:

Retrieve the setter.

attribute.setter # :name=

Returns:

  • (Symbol)

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

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.


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

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