Class: Virtus::AttributeSet

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

Overview

A set of Attribute objects

Class Method Summary collapse

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: [])


23
24
25
26
27
28
# File 'lib/virtus/attribute_set.rb', line 23

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

Class Method Details

.create(descendant) ⇒ Object

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.



8
9
10
11
12
13
# File 'lib/virtus/attribute_set.rb', line 8

def self.create(descendant)
  if descendant.respond_to?(:superclass) && descendant.superclass.respond_to?(:attribute_set)
    parent = descendant.superclass.public_send(:attribute_set)
  end
  descendant.instance_variable_set('@attribute_set', AttributeSet.new(parent))
end

Instance Method Details

#<<(attribute) ⇒ self

Adds an attribute to the set

Examples:

attribute_set << attribute

Parameters:

Returns:

  • (self)


75
76
77
78
79
# File 'lib/virtus/attribute_set.rb', line 75

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

#[](name) ⇒ Attribute

Get an attribute by name

Examples:

attribute_set[:name]  # => Attribute object

Parameters:

  • name (Symbol)

Returns:



91
92
93
# File 'lib/virtus/attribute_set.rb', line 91

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

#[]=(name, attribute) ⇒ Attribute

Set an attribute by name

Examples:

attribute_set[:name] = attribute

Parameters:

Returns:



106
107
108
109
# File 'lib/virtus/attribute_set.rb', line 106

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

#coerce(attributes) ⇒ Hash

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.

Coerce attributes received to a hash

Returns:

  • (Hash)


195
196
197
198
199
# File 'lib/virtus/attribute_set.rb', line 195

def coerce(attributes)
  ::Hash.try_convert(attributes) or raise(
    NoMethodError, "Expected #{attributes.inspect} to respond to #to_hash"
  )
end

#define_reader_method(attribute, method_name, visibility) ⇒ 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.

Defines an attribute reader method

Parameters:

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

Returns:

  • (undefined)


131
132
133
134
# File 'lib/virtus/attribute_set.rb', line 131

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

#define_writer_method(attribute, method_name, visibility) ⇒ 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.

Defines an attribute writer method

Parameters:

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

Returns:

  • (undefined)


145
146
147
148
# File 'lib/virtus/attribute_set.rb', line 145

def define_writer_method(attribute, method_name, visibility)
  define_method(method_name) { |value| attribute.set(self, value) }
  send(visibility, method_name)
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)


44
45
46
47
48
# File 'lib/virtus/attribute_set.rb', line 44

def each
  return to_enum unless block_given?
  @index.each { |name, attribute| yield attribute if name.kind_of?(Symbol) }
  self
end

#finalizeObject

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.



202
203
204
205
206
# File 'lib/virtus/attribute_set.rb', line 202

def finalize
  each do |attribute|
    self << attribute.finalize unless attribute.finalized?
  end
end

#get(object) ⇒ Hash

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.

Get values of all attributes defined for this class, ignoring privacy

Returns:

  • (Hash)


155
156
157
158
159
160
# File 'lib/virtus/attribute_set.rb', line 155

def get(object)
  each_with_object({}) do |attribute, attributes|
    name = attribute.name
    attributes[name] = object.__send__(name) if attribute.public_reader?
  end
end

#merge(attributes) ⇒ self

Adds the attributes to the set

Examples:

attribute_set.merge(attributes)

Parameters:

Returns:

  • (self)


60
61
62
63
# File 'lib/virtus/attribute_set.rb', line 60

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)


116
117
118
119
120
# File 'lib/virtus/attribute_set.rb', line 116

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

#set(object, attributes) ⇒ Hash

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.

Mass-assign attribute values

Returns:

  • (Hash)

See Also:

  • InstanceMethods#attributes=


169
170
171
172
173
174
175
176
# File 'lib/virtus/attribute_set.rb', line 169

def set(object, attributes)
  coerce(attributes).each do |name, value|
    writer_name = "#{name}="
    if object.allowed_writer_methods.include?(writer_name)
      object.__send__(writer_name, value)
    end
  end
end

#set_defaults(object, filter = method(:skip_default?)) ⇒ 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.

Set default attributes

Returns:

  • (self)


183
184
185
186
187
188
# File 'lib/virtus/attribute_set.rb', line 183

def set_defaults(object, filter = method(:skip_default?))
  each do |attribute|
    next if filter.call(object, attribute)
    attribute.set_default_value(object)
  end
end