Class: Axe::API::ValueObject
- Inherits:
-
Object
- Object
- Axe::API::ValueObject
show all
- Defined in:
- lib/axe/api/value_object.rb
Constant Summary
collapse
- BOOLEAN_TRUE_STRINGS =
%w[1 t T true TRUE].freeze
- BOOLEAN_FALSE_STRINGS =
%w[0 f F false FALSE].freeze
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attrs = {}) ⇒ ValueObject
Returns a new instance of ValueObject.
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/axe/api/value_object.rb', line 19
def initialize(attrs = {})
attrs ||= {}
self.class.attributes.each do |name, type|
raw = if attrs.respond_to?(:key?)
if attrs.key?(name)
attrs[name]
elsif attrs.key?(name.to_s)
attrs[name.to_s]
end
elsif attrs.respond_to?(name)
attrs.public_send(name)
end
instance_variable_set("@#{name}", coerce(raw, type))
end
end
|
Class Method Details
.attribute(name, type = nil) ⇒ Object
13
14
15
16
|
# File 'lib/axe/api/value_object.rb', line 13
def attribute(name, type = nil)
attributes[name] = type
attr_reader(name) unless method_defined?(name)
end
|
.attributes ⇒ Object
5
6
7
|
# File 'lib/axe/api/value_object.rb', line 5
def attributes
@attributes ||= superclass.respond_to?(:attributes) ? superclass.attributes.dup : {}
end
|
.values(&block) ⇒ Object
9
10
11
|
# File 'lib/axe/api/value_object.rb', line 9
def values(&block)
instance_eval(&block)
end
|
Instance Method Details
#==(other) ⇒ Object
Also known as:
eql?
49
50
51
|
# File 'lib/axe/api/value_object.rb', line 49
def ==(other)
other.instance_of?(self.class) && attributes == other.attributes
end
|
#[](name) ⇒ Object
63
64
65
66
|
# File 'lib/axe/api/value_object.rb', line 63
def [](name)
key = name.to_sym
self.class.attributes.key?(key) ? public_send(key) : nil
end
|
#attributes ⇒ Object
35
36
37
38
39
|
# File 'lib/axe/api/value_object.rb', line 35
def attributes
self.class.attributes.each_key.each_with_object({}) do |name, hash|
hash[name] = public_send(name)
end
end
|
#hash ⇒ Object
54
55
56
|
# File 'lib/axe/api/value_object.rb', line 54
def hash
[self.class, attributes].hash
end
|
#inspect ⇒ Object
58
59
60
61
|
# File 'lib/axe/api/value_object.rb', line 58
def inspect
pairs = attributes.map { |name, value| "#{name}=#{value.inspect}" }
pairs.empty? ? "#<#{self.class.name}>" : "#<#{self.class.name} #{pairs.join(" ")}>"
end
|
#to_h ⇒ Object
41
42
43
|
# File 'lib/axe/api/value_object.rb', line 41
def to_h
attributes
end
|
#to_hash ⇒ Object
45
46
47
|
# File 'lib/axe/api/value_object.rb', line 45
def to_hash
to_h
end
|