Class: Amfetamine::Base
- Inherits:
-
Object
show all
- Extended by:
- ActiveModel::Callbacks, ActiveModel::Naming
- Includes:
- ActiveModel::Conversion, ActiveModel::Serialization, ActiveModel::Serializers::JSON, ActiveModel::Validations, QueryMethods, Relationships, RestHelpers, TestHelpers
- Defined in:
- lib/amfetamine/base.rb
Constant Summary
Constants included
from RestHelpers
RestHelpers::RESPONSE_STATUSES
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
included
#belongs_to_relationship?, #belongs_to_relationships, included
#clean_cache!, #destroy, included, #new?, #save, #to_cacheable, #update_attributes
#handle_response, included, #rest_path, #singular_path
Constructor Details
#initialize(args = {}) ⇒ Base
Base method for creating objects
118
119
120
121
122
123
124
125
126
|
# File 'lib/amfetamine/base.rb', line 118
def initialize(args={})
super
@attributes = {}
self.set_dynamic_attributes(args.keys)
self.cache_key = self.class.recent_cache_key args.each { |k,v| self.send("#{k}=", v) }
@notsaved = true
self
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
26
27
28
|
# File 'lib/amfetamine/base.rb', line 26
def attributes
@attributes
end
|
#cache_key ⇒ Object
Returns the value of attribute cache_key.
27
28
29
|
# File 'lib/amfetamine/base.rb', line 27
def cache_key
@cache_key
end
|
Class Method Details
.amfetamine_attributes(*attrs) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/amfetamine/base.rb', line 50
def self.amfetamine_attributes(*attrs)
attrs.each do |attr|
define_method("#{attr}=") do |arg|
@attributes[attr.to_s] = arg
end
define_method("#{attr}") do
@attributes[attr.to_s]
end
end
end
|
77
78
79
80
81
|
# File 'lib/amfetamine/base.rb', line 77
def self.amfetamine_configure(hash)
hash.each do |k,v|
self.send("#{k.to_s}=", v)
end
end
|
.build_object(args) ⇒ Object
Builds an object from JSON, later on will need more (maybe object id? Or should that go in find?) It parses the hash, builds the objects and sets new to false
85
86
87
88
89
90
91
|
# File 'lib/amfetamine/base.rb', line 85
def self.build_object(args)
args = normalize_cache_data(args)
obj = self.new(args)
obj.tap { |obj| obj.instance_variable_set('@notsaved',false) } end
|
.cacheable? ⇒ Boolean
Checks if object is cachable
160
161
162
163
164
165
166
167
168
|
# File 'lib/amfetamine/base.rb', line 160
def self.cacheable?
if @disable_caching == true
false
elsif Amfetamine::Config.disable_caching == true
false
else
true
end
end
|
.class_name ⇒ Object
201
202
203
|
# File 'lib/amfetamine/base.rb', line 201
def self.class_name
self.name.downcase
end
|
.disable_caching=(value) ⇒ Object
113
114
115
|
# File 'lib/amfetamine/base.rb', line 113
def self.disable_caching=(value)
@disable_caching = value
end
|
.memcached_instance=(value, options = {}) ⇒ Object
Allows you to override the global caching server
105
106
107
108
109
110
111
|
# File 'lib/amfetamine/base.rb', line 105
def self.memcached_instance=(value, options={})
if value.is_a?(Array)
@cache_server = Amfetamine::Cache.new(value.shift, value.first) else
@cache_server = Amfetamine::Cache.new(value, options)
end
end
|
Instance Method Details
#==(other) ⇒ Object
We need to redefine this so it doesn’t check on object_id
183
184
185
186
187
188
189
|
# File 'lib/amfetamine/base.rb', line 183
def ==(other)
return false unless self.id == other.id
self.attributes.all? do |k,v|
self.attributes[k] == other.attributes[k]
end
end
|
#cacheable? ⇒ Boolean
170
171
172
|
# File 'lib/amfetamine/base.rb', line 170
def cacheable?
self.class.cacheable?
end
|
#cached? ⇒ Boolean
Checks if object is cached by checking if a SINGULAR request was made to this object.
155
156
157
|
# File 'lib/amfetamine/base.rb', line 155
def cached?
self.cache_key ? cache.get(self.cache_key).present? : false
end
|
#class_name ⇒ Object
197
198
199
|
# File 'lib/amfetamine/base.rb', line 197
def class_name
self.class.class_name
end
|
#errors ⇒ Object
191
192
193
|
# File 'lib/amfetamine/base.rb', line 191
def errors
@errors ||= ActiveModel::Errors.new(self)
end
|
#id ⇒ Object
33
34
35
|
# File 'lib/amfetamine/base.rb', line 33
def id
@attributes['id']
end
|
#is_attribute?(attr) ⇒ Boolean
128
129
130
|
# File 'lib/amfetamine/base.rb', line 128
def is_attribute?(attr)
@attributes.keys.include?(attr.to_sym)
end
|
#persisted? ⇒ Boolean
132
133
134
|
# File 'lib/amfetamine/base.rb', line 132
def persisted?
!new?
end
|
#set_dynamic_attributes(attrs = []) ⇒ Object
Sets attributes dynamically
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/amfetamine/base.rb', line 63
def set_dynamic_attributes(attrs = [])
klass = class << self;self end
attrs.each do |attr|
klass.send(:define_method, "#{attr}=") do |arg|
@attributes[attr.to_s] = arg
end
klass.send(:define_method, "#{attr}") do
@attributes[attr.to_s]
end
end
end
|
#to_hash ⇒ Object
37
38
39
|
# File 'lib/amfetamine/base.rb', line 37
def to_hash
attributes
end
|
#to_hash_with_head ⇒ Object
41
42
43
|
# File 'lib/amfetamine/base.rb', line 41
def to_hash_with_head
{class_name.to_sym => to_hash}
end
|
#to_json(*gen) ⇒ Object
140
141
142
143
144
|
# File 'lib/amfetamine/base.rb', line 140
def to_json(*gen)
options = {}
options.merge!(:root => self.class.model_name.element)
super(self.as_json(options))
end
|
#to_key ⇒ Object
146
147
148
|
# File 'lib/amfetamine/base.rb', line 146
def to_key
persisted? ? [id] : nil
end
|
#to_model ⇒ Object
136
137
138
|
# File 'lib/amfetamine/base.rb', line 136
def to_model
self
end
|
#to_param ⇒ Object
150
151
152
|
# File 'lib/amfetamine/base.rb', line 150
def to_param
persisted? ? id.to_s : nil
end
|
#update_attributes_from_response(args) ⇒ Object
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/amfetamine/base.rb', line 93
def update_attributes_from_response(args)
if args && args.is_a?(Hash) && args.has_key?(self.class_name)
args = args[self.class_name]
args.each { |k,v| self.send("#{k}=", v); self.attributes[k.to_sym] = v }
end
end
|
#valid? ⇒ Boolean
Checks to see if an object is valid or not
175
176
177
178
179
180
|
# File 'lib/amfetamine/base.rb', line 175
def valid?
errors.clear
run_callbacks(:validation) do
run_validations!
end
end
|