Class: UserManagementApi::Entity
- Inherits:
-
Object
- Object
- UserManagementApi::Entity
show all
- Defined in:
- lib/user_management_api/entity.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attrs = {}) ⇒ Entity
Returns a new instance of Entity.
40
41
42
|
# File 'lib/user_management_api/entity.rb', line 40
def initialize(attrs = {})
populate_from_json(attrs)
end
|
Class Method Details
.attribute(*attrs) ⇒ Object
Also known as:
attributes
19
20
21
22
23
24
|
# File 'lib/user_management_api/entity.rb', line 19
def self.attribute(*attrs)
attrs.compact.map { |a| a.to_sym }.each do |a|
attr_accessor a
self._properties << a
end
end
|
.entity_attribute(*args) ⇒ Object
Also known as:
entity_attributes
9
10
11
12
13
14
15
16
17
|
# File 'lib/user_management_api/entity.rb', line 9
def self.entity_attribute(*args)
klass = args.pop
raise "Invalid arguments" unless klass && klass.is_a?(Class) && args.length > 0
args.compact.map { |a| a.to_sym }.each do |a|
attr_accessor a
self._entity_properties[a.to_sym] = klass
end
end
|
.inherited(subclass) ⇒ Object
Copy properties on inheritance.
32
33
34
35
36
37
38
|
# File 'lib/user_management_api/entity.rb', line 32
def self.inherited(subclass)
entities = _entity_properties.dup
attrs = _properties.dup
subclass._entity_properties = entities.each { |k, v| entities[k] = v.dup }
subclass._properties = attrs
super
end
|
Instance Method Details
#==(other) ⇒ Object
Also known as:
eql?
82
83
84
|
# File 'lib/user_management_api/entity.rb', line 82
def ==(other)
other.class == self.class && other.__state == __state
end
|
#as_json(options = nil) ⇒ Object
44
45
46
|
# File 'lib/user_management_api/entity.rb', line 44
def as_json(options = nil)
serialize_attributes(_properties + _entity_properties.keys)
end
|
#hash ⇒ Object
88
89
90
|
# File 'lib/user_management_api/entity.rb', line 88
def hash
state.hash
end
|
#populate_from_json(attrs) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/user_management_api/entity.rb', line 52
def populate_from_json(attrs)
return unless attrs
attrs.each do |k, v|
meth = "#{k}=".to_sym
if self.respond_to? meth
if entity_class = _entity_properties[k.to_sym]
assign_entity(meth, v, entity_class)
else
self.send(meth, v)
end
end
end
end
|
#serialize_attributes(attrs) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/user_management_api/entity.rb', line 67
def serialize_attributes(attrs)
json = {}
attrs.each do |k|
meth = k.to_sym
if self.respond_to?(meth)
if _entity_properties[k.to_sym]
json[k] = serialize_entity(meth)
else
json[k] = self.send(meth)
end
end
end
json
end
|
#to_json(options = nil) ⇒ Object
48
49
50
|
# File 'lib/user_management_api/entity.rb', line 48
def to_json(options = nil)
MultiJson.dump(as_json(options))
end
|