Module: LazyResource::Resource

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Conversion, Attributes, Mapping, Types, UrlGeneration
Defined in:
lib/lazy_resource/resource.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary

Attributes included from Mapping

#fetched, #other_attributes, #persisted, #request_error

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributes

#primary_key

Methods included from Mapping

#fetched?, #load, root_node_name

Methods included from UrlGeneration

#collection_path, #collection_url, #element_path, #element_url, #new_element_path, #split_options

Class Method Details

.default_headersObject



26
27
28
# File 'lib/lazy_resource/resource.rb', line 26

def self.default_headers
  @default_headers || {}
end

.default_headers=(headers) ⇒ Object



22
23
24
# File 'lib/lazy_resource/resource.rb', line 22

def self.default_headers=(headers)
  @default_headers = headers
end

.root_node_name=(node_name) ⇒ Object



30
31
32
# File 'lib/lazy_resource/resource.rb', line 30

def self.root_node_name=(node_name)
  LazyResource::Mapping.root_node_name = node_name
end

.siteObject



18
19
20
# File 'lib/lazy_resource/resource.rb', line 18

def self.site
  @site
end

.site=(site) ⇒ Object



14
15
16
# File 'lib/lazy_resource/resource.rb', line 14

def self.site=(site)
  @site = site
end

Instance Method Details

#==(other) ⇒ Object

Tests for equality. Returns true iff other is the same object or other is an instance of the same class and has the same attributes.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/lazy_resource/resource.rb', line 126

def ==(other)
  return true if other.equal?(self)
  return false unless other.instance_of?(self.class)

  self.class.attributes.inject(true) do |memo, attribute|
    attribute_name = attribute.first
    attribute_type = attribute.last[:type]

    # Skip associations
    if attribute_type.include?(LazyResource::Resource) || (attribute_type.is_a?(::Array) && attribute_type.first.include?(LazyResource::Resource))
      memo
    else
      memo && self.send(:"#{attribute_name}") == other.send(:"#{attribute_name}")
    end
  end
end

#as_json(options = {}) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/lazy_resource/resource.rb', line 206

def as_json(options={})
  self.class.attributes.inject({}) do |hash, (attribute_name, attribute_options)|
    attribute_type = attribute_options[:type]

    # Skip nil attributes (need to use instance_variable_get to avoid the stub relations that get added for associations."
    unless self.instance_variable_get("@#{attribute_name}").nil?
      value = self.send(:"#{attribute_name}")

      if (attribute_type.is_a?(::Array) && attribute_type.first.include?(LazyResource::Resource))
        value = value.map { |v| v.as_json }
      elsif attribute_type.include?(LazyResource::Resource)
        value = value.as_json
      elsif attribute_type == DateTime
        if options[:include_time_ago_in_words] && defined?(TwitterCldr)
          hash[:"#{attribute_name}_in_words"] = (DateTime.now - (DateTime.now - value).to_f).localize.ago.to_s
        end

        if options[:strftime]
          value = self.send(attribute_name).strftime(options[:strftime])
        end

        if options[attribute_name.to_sym] && options[attribute_name.to_sym][:strftime]
          value = self.send(attribute_name).strftime(options[attribute_name.to_sym][:strftime])
        end

        value = value.to_s unless value.is_a?(String)
      end

      hash[attribute_name.to_sym] = value
    end
    hash
  end
end

#attribute_paramsObject



198
199
200
201
202
203
204
# File 'lib/lazy_resource/resource.rb', line 198

def attribute_params
  { self.class.element_name.to_sym => changed_attributes.inject({}) do |hash, changed_attribute|
    hash.tap do |hash|
      hash[changed_attribute.first] = self.send(changed_attribute.first)
    end
  end }
end

#createObject



165
166
167
168
169
170
171
172
# File 'lib/lazy_resource/resource.rb', line 165

def create
  run_callbacks :create do
    request = Request.new(self.collection_url, self, { :method => :post, :body => attribute_params.to_json })
    self.class.request_queue.queue(request)
    self.class.fetch_all
    self.changed_attributes.clear
  end
end

#destroyObject



183
184
185
186
187
188
189
# File 'lib/lazy_resource/resource.rb', line 183

def destroy
  run_callbacks :destroy do
    request = Request.new(self.element_url, self, { :method => :delete })
    self.class.request_queue.queue(request)
    self.class.fetch_all
  end
end

#eql?(other) ⇒ Boolean

Returns:



143
144
145
# File 'lib/lazy_resource/resource.rb', line 143

def eql?(other)
  self == other
end

#initialize(attributes = {}) ⇒ Object



118
119
120
121
122
# File 'lib/lazy_resource/resource.rb', line 118

def initialize(attributes={})
  self.tap do |resource|
    resource.load(attributes, false)
  end
end

#new_record?Boolean Also known as: new?

Returns:



151
152
153
# File 'lib/lazy_resource/resource.rb', line 151

def new_record?
  !persisted?
end

#persisted?Boolean

Returns:



147
148
149
# File 'lib/lazy_resource/resource.rb', line 147

def persisted?
  @persisted
end

#saveObject



157
158
159
160
161
162
163
# File 'lib/lazy_resource/resource.rb', line 157

def save
  return true if !changed?
  run_callbacks :save do
    new_record? ? create : update
    self.persisted = true
  end
end

#updateObject



174
175
176
177
178
179
180
181
# File 'lib/lazy_resource/resource.rb', line 174

def update
  run_callbacks :update do
    request = Request.new(self.element_url, self, { :method => :put, :body => attribute_params.to_json })
    self.class.request_queue.queue(request)
    self.class.fetch_all
    self.changed_attributes.clear
  end
end

#update_attributes(attributes = {}) ⇒ Object



191
192
193
194
195
196
# File 'lib/lazy_resource/resource.rb', line 191

def update_attributes(attributes={})
  attributes.each do |name, value|
    self.send("#{name}=", value)
  end
  self.update
end