Class: WrAPI::Request::Entity
- Inherits:
-
Object
- Object
- WrAPI::Request::Entity
show all
- Defined in:
- lib/wrapi/entity.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attr) ⇒ Entity
Returns a new instance of Entity.
16
17
18
19
20
21
22
23
|
# File 'lib/wrapi/entity.rb', line 16
def initialize(attr)
case attr
when Hash
@attributes = attr.clone.transform_keys(&:to_s)
else
@attributes = attr.clone
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *arguments, &block) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/wrapi/entity.rb', line 33
def method_missing(method_sym, *arguments, &block)
assignment = method_sym[/.*(?==\z)/m]
if assignment
raise ArgumentError, "wrong number of arguments (given #{arguments.length}, expected 1)", caller(1) unless arguments.length == 1
@attributes[assignment] = arguments[0]
elsif @attributes.include? method_sym.to_s
accessor(method_sym.to_s)
else
@attributes.send(method_sym, *arguments, &block)
end
end
|
Class Method Details
.create(attr) ⇒ Object
factory method to create entity or array of entities
8
9
10
11
12
13
14
|
# File 'lib/wrapi/entity.rb', line 8
def self.create(attr)
if attr.is_a? Array
Entity.entify(attr)
else
Entity.new(attr) if attr
end
end
|
.entify(a) ⇒ Object
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/wrapi/entity.rb', line 84
def self.entify(a)
if ( a.count > 0 ) && ( a.first.is_a? Hash )
a.dup.map do |item|
Entity.create(item)
end
else
a
end
end
|
Instance Method Details
#==(other) ⇒ Object
Also known as:
eql?
79
80
81
|
# File 'lib/wrapi/entity.rb', line 79
def ==(other)
(self.class == other.class) && (self.attributes.eql? other.attributes)
end
|
#accessor(method) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/wrapi/entity.rb', line 61
def accessor(method)
case @attributes[method]
when Hash
@attributes[method] = self.class.new(@attributes[method])
when Array
@attributes[method] = Entity.entify(@attributes[method])
else
@attributes[method]
end
end
|
#attributes ⇒ Object
25
26
27
|
# File 'lib/wrapi/entity.rb', line 25
def attributes
@attributes || {}
end
|
#attributes=(val) ⇒ Object
29
30
31
|
# File 'lib/wrapi/entity.rb', line 29
def attributes= val
@attributes = val || {}
end
|
#clone ⇒ Object
73
74
75
76
77
|
# File 'lib/wrapi/entity.rb', line 73
def clone
c = super
c.attributes = @attributes.clone
c
end
|
#respond_to?(method_sym, include_private = false) ⇒ Boolean
48
49
50
51
52
53
54
55
|
# File 'lib/wrapi/entity.rb', line 48
def respond_to?(method_sym, include_private = false)
@attributes ||= {}
if @attributes.include? method_sym.to_s
true
else
@attributes.respond_to?(method_sym, include_private)
end
end
|
#to_json(options = {}) ⇒ Object
57
58
59
|
# File 'lib/wrapi/entity.rb', line 57
def to_json(options = {})
@attributes.to_json
end
|