module Datmachine::Resource

Attributes

attributes[RW]
href[RW]
id[RW]

Public Class Methods

included(base) click to toggle source
# File lib/datmachine/resources/resource.rb, line 191
def self.included(base)
  base.extend ClassMethods
end
new(attributes = {}) click to toggle source
# File lib/datmachine/resources/resource.rb, line 18
def initialize(attributes = {})
  @attributes = Utils.indifferent_read_access attributes
  @hyperlinks = {}
end

Public Instance Methods

copy_from(other) click to toggle source
# File lib/datmachine/resources/resource.rb, line 139
def copy_from(other)
  other.instance_variables.each do |ivar|
    instance_variable_set ivar, other.instance_variable_get(ivar)
  end
end
destroy()
Alias for: unstore
does_resource_respond_to?(method_name) click to toggle source
# File lib/datmachine/resources/resource.rb, line 187
def does_resource_respond_to?(method_name)
  @attributes.has_key?(method_name.to_s) or @hyperlinks.has_key?(method_name.to_s)
end
fetch(*arguments) click to toggle source

delegate the query to the pager module

# File lib/datmachine/resources/resource.rb, line 85
def fetch(*arguments)
  self.class.find *arguments
end
Also aliased as: find
find(*arguments)
Alias for: fetch
hydrate(links, meta) click to toggle source
# File lib/datmachine/resources/resource.rb, line 43
def hydrate(links, meta)
  # links here is 'marketplaces.events'
  links.each do |key, uri|
    # key is 'marketplaces.events'
    property = key.sub(/.*?\./, '')
    # property here is events
    # if marketplace.links is a hash
    # for each property and uri, we want to set them on the instance
    # as pagers.
    #
    # right?
    if self.links.include? property
      link_value = self.links[property]
      # expands /customers/{marketplaces.owner_customer} to /customers/ACxxxxx
      template = Addressable::Template.new(uri)
      uri = template.expand("#{key}" => link_value).to_s
      @hyperlinks[property] = Datmachine::Utils.callable(
          link_value.nil? ? link_value : lambda { self.class.find(uri) }
      )
    else
      unless uri.nil? || uri.is_a?(Hash)
        # matches something like '/blah/{class.attribute}/bliakdf'
        begin
          match_data = /\{(#{self.class.collection_name}\.(\w+))\}/.match(uri)
        rescue TypeError => ex
          puts 'what the fuck'
          raise ex
        end
        unless match_data.nil?
          attribute_path = match_data[1]
          attribute_name = match_data[2]
          template = Addressable::Template.new(uri)
          uri = template.expand("#{attribute_path}" => @attributes[attribute_name]).to_s
        end
      end
      @hyperlinks[property] = Datmachine::Utils.callable(Datmachine::Pager.new(uri, {}))
    end
  end
end
id=(value) click to toggle source
# File lib/datmachine/resources/resource.rb, line 27
def id=(value)
  attributes[:id] = value
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/datmachine/resources/resource.rb, line 145
def method_missing(method, *args, &block)
  if @attributes.has_key?(method.to_s)
    return @attributes[method.to_s]
  end

  case method.to_s
    when /(.+)=$/
      attr = method.to_s.chop
      @attributes[attr] = args[0]
    else
      # This piece of code is a bit disgusting. We will clean it up soon,
      # but basically, we were creating closures using this code snippet
      # but those closures were transferred to the actual classes themselves
      # so you would have something like BankAccount.new.account and this
      # will give the last closure added for a BankAccount even if it has
      # nothing to do with the actual class itself.
      #
      # This caused some weird errors, so the best thing to do was to just
      # move this piece of code and "dynamically" enable it for all
      # method requests that are essentially #{method}_uri.
      #
      # This solves the acute problem, for now.
      if @hyperlinks.has_key? "#{method}"
        value = @hyperlinks["#{method}"]
        result = value.call
        return result
      else
        super
      end
  end
end
reload(the_response = nil) click to toggle source
# File lib/datmachine/resources/resource.rb, line 128
def reload(the_response = nil)
  if the_response
    return if the_response.body.to_s.length.zero?
    fresh = self.class.construct_from_response the_response.body
  else
    fresh = self.find(@attributes[:href])
  end
  fresh and copy_from fresh
  return fresh
end
respond_to?(method_name, include_private=false) click to toggle source
Calls superclass method
# File lib/datmachine/resources/resource.rb, line 178
def respond_to?(method_name, include_private=false)
  does_resource_respond_to?(method_name) || super
end
respond_to_missing?(method_name, include_private=false) click to toggle source
Calls superclass method
# File lib/datmachine/resources/resource.rb, line 182
def respond_to_missing?(method_name, include_private=false)
  does_resource_respond_to?(method_name) || super
end
sanitize() click to toggle source
# File lib/datmachine/resources/resource.rb, line 107
def sanitize
  to_submit = {}
  @attributes.each do |key, value|
    unless value.is_a? Datmachine::Resource
      to_submit[key] = value
    end
  end
  to_submit
end
save() click to toggle source
# File lib/datmachine/resources/resource.rb, line 91
def save
  href = @attributes.delete('href')
  method = :post
  if href.nil?
    href = self.class.collection_path
  elsif !Datmachine.is_collection(href)
    method = :put
  end

  attributes_to_submit = self.sanitize
  @response = Datmachine.send(method, href, attributes_to_submit)
  @attributes['href'] = href

  reload @response
end
unstore() click to toggle source
# File lib/datmachine/resources/resource.rb, line 123
def unstore
  Datmachine.unstore @attributes[:href]
end
Also aliased as: destroy

Private Instance Methods

response() click to toggle source
# File lib/datmachine/resources/resource.rb, line 117
def response
  @response
end