Class: Virtus::AttributeSet
- Inherits:
-
Module
- Object
- Module
- Virtus::AttributeSet
- Includes:
- Enumerable
- Defined in:
- lib/virtus/attribute_set.rb
Overview
A set of Attribute objects
Class Method Summary collapse
- .create(descendant) ⇒ Object private
Instance Method Summary collapse
-
#<<(attribute) ⇒ self
Adds an attribute to the set.
-
#[](name) ⇒ Attribute
Get an attribute by name.
-
#[]=(name, attribute) ⇒ Attribute
Set an attribute by name.
-
#coerce(attributes) ⇒ Hash
private
Coerce attributes received to a hash.
-
#define_reader_method(attribute, method_name, visibility) ⇒ undefined
private
Defines an attribute reader method.
-
#define_writer_method(attribute, method_name, visibility) ⇒ undefined
private
Defines an attribute writer method.
-
#each {|attribute| ... } ⇒ self
Iterate over each attribute in the set.
- #finalize ⇒ Object private
-
#get(object) ⇒ Hash
private
Get values of all attributes defined for this class, ignoring privacy.
-
#initialize(parent = nil, attributes = []) ⇒ undefined
constructor
private
Initialize an AttributeSet.
-
#merge(attributes) ⇒ self
Adds the attributes to the set.
-
#reset ⇒ self
private
Reset the index when the parent is updated.
-
#set(object, attributes) ⇒ Hash
private
Mass-assign attribute values.
-
#set_defaults(object, filter = method(:skip_default?)) ⇒ self
private
Set default attributes.
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
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
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
91 92 93 |
# File 'lib/virtus/attribute_set.rb', line 91 def [](name) @index[name] end |
#[]=(name, attribute) ⇒ Attribute
Set an attribute by name
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
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
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
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
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 |
#finalize ⇒ 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.
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
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
60 61 62 63 |
# File 'lib/virtus/attribute_set.rb', line 60 def merge(attributes) attributes.each { |attribute| self << attribute } self end |
#reset ⇒ 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.
Reset the index when the parent is updated
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
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
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 |