Class: DuckRecord::AttributeSet
- Inherits:
-
Object
- Object
- DuckRecord::AttributeSet
- Defined in:
- lib/duck_record/attribute_set.rb,
lib/duck_record/attribute_set/yaml_encoder.rb
Overview
:nodoc:
Defined Under Namespace
Classes: YAMLEncoder
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #deep_dup ⇒ Object
-
#fetch_value(name) ⇒ Object
This form is significantly faster on JRuby, and this is one of our biggest hotspots.
- #freeze ⇒ Object
-
#initialize(attributes) ⇒ AttributeSet
constructor
A new instance of AttributeSet.
- #initialize_clone(_) ⇒ Object
- #initialize_dup(_) ⇒ Object
- #key?(name) ⇒ Boolean
- #keys ⇒ Object
- #map(&block) ⇒ Object
- #to_hash ⇒ Object (also: #to_h)
- #values_before_type_cast ⇒ Object
- #write_cast_value(name, value) ⇒ Object
- #write_from_user(name, value) ⇒ Object
Constructor Details
#initialize(attributes) ⇒ AttributeSet
Returns a new instance of AttributeSet.
7 8 9 |
# File 'lib/duck_record/attribute_set.rb', line 7 def initialize(attributes) @attributes = attributes end |
Instance Method Details
#==(other) ⇒ Object
82 83 84 |
# File 'lib/duck_record/attribute_set.rb', line 82 def ==(other) attributes == other.attributes end |
#[](name) ⇒ Object
11 12 13 |
# File 'lib/duck_record/attribute_set.rb', line 11 def [](name) attributes[name] || Attribute.null(name) end |
#[]=(name, value) ⇒ Object
15 16 17 |
# File 'lib/duck_record/attribute_set.rb', line 15 def []=(name, value) attributes[name] = value end |
#deep_dup ⇒ Object
61 62 63 64 65 |
# File 'lib/duck_record/attribute_set.rb', line 61 def deep_dup dup.tap do |copy| copy.instance_variable_set(:@attributes, attributes.deep_dup) end end |
#fetch_value(name) ⇒ Object
This form is significantly faster on JRuby, and this is one of our biggest hotspots. github.com/jruby/jruby/pull/2562
39 40 41 |
# File 'lib/duck_record/attribute_set.rb', line 39 def fetch_value(name, &block) self[name].value(&block) end |
#freeze ⇒ Object
56 57 58 59 |
# File 'lib/duck_record/attribute_set.rb', line 56 def freeze @attributes.freeze super end |
#initialize_clone(_) ⇒ Object
72 73 74 75 |
# File 'lib/duck_record/attribute_set.rb', line 72 def initialize_clone(_) @attributes = attributes.clone super end |
#initialize_dup(_) ⇒ Object
67 68 69 70 |
# File 'lib/duck_record/attribute_set.rb', line 67 def initialize_dup(_) @attributes = attributes.dup super end |
#key?(name) ⇒ Boolean
28 29 30 |
# File 'lib/duck_record/attribute_set.rb', line 28 def key?(name) attributes.key?(name) && self[name].initialized? end |
#keys ⇒ Object
32 33 34 |
# File 'lib/duck_record/attribute_set.rb', line 32 def keys attributes.each_key.select { |name| self[name].initialized? } end |
#map(&block) ⇒ Object
77 78 79 80 |
# File 'lib/duck_record/attribute_set.rb', line 77 def map(&block) new_attributes = attributes.transform_values(&block) AttributeSet.new(new_attributes) end |
#to_hash ⇒ Object Also known as: to_h
23 24 25 |
# File 'lib/duck_record/attribute_set.rb', line 23 def to_hash initialized_attributes.transform_values(&:value) end |
#values_before_type_cast ⇒ Object
19 20 21 |
# File 'lib/duck_record/attribute_set.rb', line 19 def values_before_type_cast attributes.transform_values(&:value_before_type_cast) end |
#write_cast_value(name, value) ⇒ Object
52 53 54 |
# File 'lib/duck_record/attribute_set.rb', line 52 def write_cast_value(name, value) attributes[name] = self[name].with_cast_value(value) end |
#write_from_user(name, value) ⇒ Object
48 49 50 |
# File 'lib/duck_record/attribute_set.rb', line 48 def write_from_user(name, value) attributes[name] = self[name].with_value_from_user(value) end |