Class: HasEmbeddedDocument::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/has_embedded_document/base.rb

Direct Known Subclasses

ApplicationDocument

Defined Under Namespace

Classes: Attribute

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.

Parameters:

  • attributes (Hash{String => Object}) (defaults to: {})


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.

Parameters:

  • name (Symbol)
  • type (Symbol) (defaults to: :string)


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, **options)
  name = name.to_sym
  type = ActiveRecord::Type.lookup(type, **options)

  attributes[name] = Attribute.new(name, type, default).freeze

  define_method(name) { read_attribute(name) }
  define_method("#{name}=") { |value| write_attribute(name, value) }
end

.attributesHash{Symbol => Attribute}

Returns:



13
14
15
# File 'lib/has_embedded_document/base.rb', line 13

def self.attributes
  @attributes ||= {}
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


102
103
104
# File 'lib/has_embedded_document/base.rb', line 102

def ==(other)
  other.is_a?(self.class) && other.attributes == @attributes
end

#attributesHash{Symbol => Object}

Returns:

  • (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.

Parameters:

  • attributes (Hash{Symbol => Object})


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

#dupBase

Returns:



49
50
51
# File 'lib/has_embedded_document/base.rb', line 49

def dup
  self.class.new(attributes)
end

#dup_with(attributes) ⇒ Base

Parameters:

  • attributes (Hash)

Returns:



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

Parameters:

  • name (Symbol)

Raises:

  • (ArgumentError)


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

Returns:

  • (self)


91
92
93
# File 'lib/has_embedded_document/base.rb', line 91

def readonly!
  tap { @attributes.freeze }
end

#readonly?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/has_embedded_document/base.rb', line 86

def readonly?
  @attributes.frozen?
end

#to_hHash

Returns:

  • (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.

Parameters:

  • name (Symbol)
  • value (Object)

Raises:

  • (ArgumentError)


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