Module: Valued
- Defined in:
- lib/valued.rb,
lib/valued/mutable.rb,
lib/valued/version.rb
Defined Under Namespace
Modules: ClassMethods, Mutable
Constant Summary
collapse
- VERSION =
'0.4.1'
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.define(*attrs, &block) ⇒ Object
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/valued.rb', line 13
def self.define(*attrs, &block)
klass =
Class.new do
include Valued
attributes(*attrs)
end
klass.class_eval(&block) if block_given?
klass
end
|
.included(base) ⇒ Object
24
25
26
|
# File 'lib/valued.rb', line 24
def self.included(base)
base.extend(ClassMethods)
end
|
Instance Method Details
#==(other) ⇒ Object
51
52
53
54
55
|
# File 'lib/valued.rb', line 51
def ==(other)
_attributes.all? do |attribute|
other.respond_to?(attribute) && send(attribute) == other.send(attribute)
end
end
|
#eql?(other) ⇒ Boolean
57
58
59
|
# File 'lib/valued.rb', line 57
def eql?(other)
self.class == other.class && self == other
end
|
#hash ⇒ Object
61
62
63
|
# File 'lib/valued.rb', line 61
def hash
(_attributes.map { |attribute| send(attribute) } + [self.class]).hash
end
|
#initialize(attributes = {}) ⇒ Object
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/valued.rb', line 28
def initialize(attributes = {})
_attributes.each do |attribute|
if attributes.key?(attribute)
instance_variable_set(
"@#{attribute}",
attributes.fetch(attribute).freeze
)
end
end
end
|
#inspect ⇒ Object
75
76
77
78
79
80
81
|
# File 'lib/valued.rb', line 75
def inspect
inspected_attributes =
_attributes
.map { |attribute| "#{attribute}=#{send(attribute).inspect}" }
.join(' ')
"#<#{self.class} #{inspected_attributes}>"
end
|
#to_h ⇒ Object
65
66
67
68
69
|
# File 'lib/valued.rb', line 65
def to_h
_attributes.each_with_object({}) do |attribute, hash|
hash[attribute] = send(attribute)
end
end
|
#to_s ⇒ Object
71
72
73
|
# File 'lib/valued.rb', line 71
def to_s
inspect
end
|
#update(new_attributes) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/valued.rb', line 39
def update(new_attributes)
self.class.new(
_attributes.each_with_object({}) do |attribute, result|
if new_attributes.key?(attribute)
result[attribute] = new_attributes[attribute]
else
result[attribute] = self.send(attribute)
end
end
)
end
|