Class: ObjectJSONMapper::Base
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#destroy, included, #reload, #save, #update
included
Methods included from Errors
#load_errors, #valid?
#serializable_hash
Methods included from Conversion
#to_key, #to_model, #to_param, #to_partial_path
Methods included from Local
#find_by_local, included, #local
Constructor Details
#initialize(attributes = {}) ⇒ Base
Returns a new instance of Base.
21
22
23
24
|
# File 'lib/object_json_mapper/base.rb', line 21
def initialize(attributes = {})
self.attributes = attributes
@persisted = false
end
|
Class Attribute Details
.associations ⇒ Object
Returns the value of attribute associations.
60
61
62
|
# File 'lib/object_json_mapper/base.rb', line 60
def associations
@associations
end
|
.relation ⇒ Object
Returns the value of attribute relation.
60
61
62
|
# File 'lib/object_json_mapper/base.rb', line 60
def relation
@relation
end
|
.root_url ⇒ Object
Returns the value of attribute root_url.
60
61
62
|
# File 'lib/object_json_mapper/base.rb', line 60
def root_url
@root_url
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
15
16
17
|
# File 'lib/object_json_mapper/base.rb', line 15
def attributes
@attributes
end
|
#persisted ⇒ Object
Also known as:
persisted?
Returns the value of attribute persisted.
15
16
17
|
# File 'lib/object_json_mapper/base.rb', line 15
def persisted
@persisted
end
|
Class Method Details
.attribute(name, type: nil, default: nil) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/object_json_mapper/base.rb', line 90
def attribute(name, type: nil, default: nil)
define_method(name) do
return default.call if attributes.exclude?(name) && default
return type.call(attributes[name]) if type
attributes[name]
end
define_method("#{name}=") do |value|
attributes[name] = value
end
end
|
.client ⇒ Object
68
69
70
71
72
73
|
# File 'lib/object_json_mapper/base.rb', line 68
def client
RestClient::Resource.new(
URI.join(ObjectJSONMapper.base_url, root_url).to_s,
headers: ObjectJSONMapper.
)
end
|
75
76
77
|
# File 'lib/object_json_mapper/base.rb', line 75
def configure
yield self
end
|
Returns current model instance.
140
141
142
143
144
145
146
147
148
|
# File 'lib/object_json_mapper/base.rb', line 140
def find(id)
raise ActiveRecord::RecordNotFound if id.nil?
result = HTTP.parse_json(client[id].get.body)
persist(result)
rescue RestClient::ExceptionWithResponse
raise ActiveRecord::RecordNotFound
end
|
rubocop:disable Rails/FindBy
154
155
156
|
# File 'lib/object_json_mapper/base.rb', line 154
def find_by(conditions = {})
where(conditions).first
end
|
.inherited(base) ⇒ Object
62
63
64
65
66
|
# File 'lib/object_json_mapper/base.rb', line 62
def inherited(base)
base.root_url = base.name.underscore.pluralize
base.associations = Associations::Registry.new
base.relation = Relation.new(klass: base)
end
|
.name ⇒ Object
83
84
85
|
# File 'lib/object_json_mapper/base.rb', line 83
def name
@name || super
end
|
.name=(value) ⇒ Object
79
80
81
|
# File 'lib/object_json_mapper/base.rb', line 79
def name=(value)
@name = value.to_s
end
|
.none ⇒ Object
158
159
160
|
# File 'lib/object_json_mapper/base.rb', line 158
def none
NullRelation.new(klass: self)
end
|
Same as ‘new` but for persisted records
125
126
127
128
129
|
# File 'lib/object_json_mapper/base.rb', line 125
def persist(attributes = {})
new(attributes).tap do |base|
base.persisted = true
end
end
|
.root(value) ⇒ Object
115
116
117
118
119
120
|
# File 'lib/object_json_mapper/base.rb', line 115
def root(value)
clone.tap do |base|
base.name = name
base.root_url = value.to_s
end
end
|
.scope(name, block) ⇒ Object
105
106
107
108
109
110
111
112
113
|
# File 'lib/object_json_mapper/base.rb', line 105
def scope(name, block)
define_singleton_method(name) do
relation.deep_clone.instance_exec(&block)
end
relation.define_singleton_method(name) do
instance_exec(&block)
end
end
|
Returns collection of model instances.
133
134
135
|
# File 'lib/object_json_mapper/base.rb', line 133
def where(conditions = {})
relation.tap { |relation| relation.klass = self }.where(conditions)
end
|
Instance Method Details
#==(other) ⇒ Object
51
52
53
|
# File 'lib/object_json_mapper/base.rb', line 51
def ==(other)
attributes == other.attributes && persisted == other.persisted
end
|
#client ⇒ Object
55
56
57
|
# File 'lib/object_json_mapper/base.rb', line 55
def client
self.class.client[id]
end
|
#new_record? ⇒ Boolean
28
29
30
|
# File 'lib/object_json_mapper/base.rb', line 28
def new_record?
!persisted?
end
|
#persist ⇒ Object
32
33
34
|
# File 'lib/object_json_mapper/base.rb', line 32
def persist
@persisted = true
end
|
#reloadable? ⇒ Boolean
36
37
38
|
# File 'lib/object_json_mapper/base.rb', line 36
def reloadable?
to_key.any?
end
|