Class: HasEmbeddedDocument::Base
- Inherits:
-
Object
- Object
- HasEmbeddedDocument::Base
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/has_embedded_document/base.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Attribute
Class Method Summary collapse
- .attribute(name, type = :string, default: nil, **options) ⇒ void
- .attributes ⇒ Hash{Symbol => Attribute}
Instance Method Summary collapse
- #==(other) ⇒ Boolean
- #attributes ⇒ Hash{Symbol => Object}
- #attributes=(attributes) ⇒ void
- #dup ⇒ Base
- #dup_with(attributes) ⇒ Base
-
#initialize(attributes = {}) ⇒ Base
constructor
A new instance of Base.
- #read_attribute(name) ⇒ Object
- #readonly! ⇒ self
- #readonly? ⇒ Boolean
- #to_h ⇒ Hash
- #write_attribute(name, value) ⇒ void
Constructor Details
#initialize(attributes = {}) ⇒ Base
Returns a new instance of Base.
31 32 33 |
# File 'lib/has_embedded_document/base.rb', line 31 def initialize(attributes = {}) @attributes = attributes.stringify_keys end |
Class Method Details
.attribute(name, type = :string, default: nil, **options) ⇒ void
This method returns an undefined value.
20 21 22 23 24 25 26 27 28 |
# File 'lib/has_embedded_document/base.rb', line 20 def self.attribute(name, type = :string, default: nil, **) name = name.to_sym type = ActiveRecord::Type.lookup(type, **) attributes[name] = Attribute.new(name, type, default).freeze define_method(name) { read_attribute(name) } define_method("#{name}=") { |value| write_attribute(name, value) } end |
.attributes ⇒ Hash{Symbol => Attribute}
13 14 15 |
# File 'lib/has_embedded_document/base.rb', line 13 def self.attributes @attributes ||= {} end |
Instance Method Details
#==(other) ⇒ Boolean
102 103 104 |
# File 'lib/has_embedded_document/base.rb', line 102 def ==(other) other.is_a?(self.class) && other.attributes == @attributes end |
#attributes ⇒ Hash{Symbol => Object}
36 37 38 |
# File 'lib/has_embedded_document/base.rb', line 36 def attributes @attributes.dup end |
#attributes=(attributes) ⇒ void
This method returns an undefined value.
42 43 44 45 46 |
# File 'lib/has_embedded_document/base.rb', line 42 def attributes=(attributes) attributes.each do |name, value| write_attribute(name, value) end end |
#dup ⇒ Base
49 50 51 |
# File 'lib/has_embedded_document/base.rb', line 49 def dup self.class.new(attributes) end |
#dup_with(attributes) ⇒ Base
55 56 57 58 59 |
# File 'lib/has_embedded_document/base.rb', line 55 def dup_with(attributes) dup.tap do |object| object.attributes = attributes end end |
#read_attribute(name) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/has_embedded_document/base.rb', line 62 def read_attribute(name) attribute = self.class.attributes[name.to_sym] raise ArgumentError, "Unknown attribute: #{name}" if attribute.nil? @attributes.fetch(name.to_s) do case (default = attribute.default) when Proc then instance_eval(&default) when Symbol then send(default) else default end end end |
#readonly! ⇒ self
91 92 93 |
# File 'lib/has_embedded_document/base.rb', line 91 def readonly! tap { @attributes.freeze } end |
#readonly? ⇒ Boolean
86 87 88 |
# File 'lib/has_embedded_document/base.rb', line 86 def readonly? @attributes.frozen? end |
#to_h ⇒ Hash
96 97 98 |
# File 'lib/has_embedded_document/base.rb', line 96 def to_h attributes end |
#write_attribute(name, value) ⇒ void
This method returns an undefined value.
78 79 80 81 82 83 |
# File 'lib/has_embedded_document/base.rb', line 78 def write_attribute(name, value) attribute = self.class.attributes[name.to_sym] raise ArgumentError, "Unknown attribute: #{name}" if attribute.nil? @attributes[name.to_s] = attribute.type.cast(value) end |