Class: Virtus::AttributeSet

Inherits:
Module
  • Object
show all
Includes:
Enumerable
Defined in:
lib/virtus/attribute_set.rb

Overview

A set of Attribute objects

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil, attributes = []) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize an AttributeSet

Parameters:

  • parent (AttributeSet) (defaults to: nil)
  • attributes (Array) (defaults to: [])


15
16
17
18
19
20
# File 'lib/virtus/attribute_set.rb', line 15

def initialize(parent = nil, attributes = [])
  @parent       = parent
  @attributes   = attributes.dup
  @index        = {}
  reset
end

Instance Method Details

#<<(attribute) ⇒ self

Adds an attribute to the set

Examples:

attribute_set << attribute

Parameters:

Returns:

  • (self)


67
68
69
70
71
# File 'lib/virtus/attribute_set.rb', line 67

def <<(attribute)
  self[attribute.name] = attribute
  attribute.define_accessor_methods(self)
  self
end

#[](name) ⇒ Attribute

Get an attribute by name

Examples:

attribute_set[:name]  # => Attribute object

Parameters:

  • name (Symbol)

Returns:



83
84
85
# File 'lib/virtus/attribute_set.rb', line 83

def [](name)
  @index[name]
end

#[]=(name, attribute) ⇒ Attribute

Set an attribute by name

Examples:

attribute_set[:name] = attribute

Parameters:

Returns:



98
99
100
101
# File 'lib/virtus/attribute_set.rb', line 98

def []=(name, attribute)
  @attributes << attribute
  update_index(name, attribute)
end

#define_reader_method(attribute, method_name, visibility) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Defines an attribute reader method

Parameters:

  • attribute (Attribute)
  • method_name (Symbol)
  • visibility (Symbol)

Returns:

  • (self)


123
124
125
126
127
# File 'lib/virtus/attribute_set.rb', line 123

def define_reader_method(attribute, method_name, visibility)
  define_method(method_name) { attribute.get(self) }
  send(visibility, method_name)
  self
end

#define_writer_method(attribute, method_name, visibility) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Defines an attribute writer method

Parameters:

  • attribute (Attribute)
  • method_name (Symbol)
  • visibility (Symbol)

Returns:

  • (self)


138
139
140
141
142
# File 'lib/virtus/attribute_set.rb', line 138

def define_writer_method(attribute, method_name, visibility)
  define_method(method_name) { |value| attribute.set(self, value) }
  send(visibility, method_name)
  self
end

#each {|attribute| ... } ⇒ self

Iterate over each attribute in the set

Examples:

attribute_set = AttributeSet.new(attributes, parent)
attribute_set.each { |attribute| ... }

Yields:

  • (attribute)

Yield Parameters:

  • attribute (Attribute)

    each attribute in the set

Returns:

  • (self)


36
37
38
39
40
# File 'lib/virtus/attribute_set.rb', line 36

def each
  return to_enum unless block_given?
  @index.values.uniq.each { |attribute| yield attribute }
  self
end

#merge(attributes) ⇒ self

Adds the attributes to the set

Examples:

attribute_set.merge(attributes)

Parameters:

Returns:

  • (self)


52
53
54
55
# File 'lib/virtus/attribute_set.rb', line 52

def merge(attributes)
  attributes.each { |attribute| self << attribute }
  self
end

#resetself

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reset the index when the parent is updated

Returns:

  • (self)


108
109
110
111
112
# File 'lib/virtus/attribute_set.rb', line 108

def reset
  merge_attributes(@parent) if @parent
  merge_attributes(@attributes)
  self
end