module Finix::Resource

Public Class Methods

included(base) click to toggle source
# File lib/finix/resources/resource.rb, line 99
def self.included(base)
  base.extend ClassMethods
end
new(*args) click to toggle source
# File lib/finix/resources/resource.rb, line 10
def initialize(*args)
  opts = args.slice!(0) || {}
  href = opts.delete(:href)
  @attributes = Finix::Utils.indifferent_read_access opts

  @hyperlinks = Finix::Utils.eval_class(self, IndifferentHash).new
  @hyperlinks[:self] = href if href =~ URI::regexp
end

Public Instance Methods

copy_from(other) click to toggle source
# File lib/finix/resources/resource.rb, line 89
def copy_from(other)
  other.instance_variables.each do |ivar|
    instance_variable_set ivar, other.instance_variable_get(ivar)
  end
end
fetch(*arguments) click to toggle source
# File lib/finix/resources/resource.rb, line 37
def fetch(*arguments)
  self.class.find *arguments
end
Also aliased as: find
find(*arguments)
Alias for: fetch
hydrate(links) click to toggle source
# File lib/finix/resources/resource.rb, line 21
def hydrate(links)
  links.each do |key, link|
    property = key.sub(/.*?\./, '')

    href = link[:href]
    if property == 'self'
      @hyperlinks[:self] = href
    else
      split_uri = Finix.split_the_href(href).reverse!
      cls = Finix.find_resource_cls split_uri[0]
      cls = cls.nil? ? Finix.from_hypermedia_registry(href) : Finix::Utils.eval_class(self, Pagination)
      @hyperlinks[property] = Finix::Utils.callable(cls.new :href => href)
    end
  end
end
refresh(the_response = nil) click to toggle source
# File lib/finix/resources/resource.rb, line 78
def refresh(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(@hyperlinks[:self])
  end
  fresh and copy_from fresh
  self
end
sanitize() click to toggle source
# File lib/finix/resources/resource.rb, line 64
def sanitize
  to_submit = {}
  @attributes.each do |key, value|
    to_submit[key] = value unless value.is_a? Finix::Resource
  end
  to_submit
end
save(options = {}, href = nil) click to toggle source
# File lib/finix/resources/resource.rb, line 43
def save(options = {}, href = nil)
  options = options.is_a?(Finix::Resource) ? options.attributes : options
  @attributes = @attributes.merge options
  href ||= @hyperlinks[:self]
  method = :post
  if href.nil?
    href = Finix.get_href self.class
  elsif not @attributes[:id].nil?
    method = :put
  end

  attributes_to_submit = self.sanitize
  begin
    @response = Finix.send(method, href, attributes_to_submit)
  rescue Exception
    raise
  end

  refresh @response
end
to_s() click to toggle source
# File lib/finix/resources/resource.rb, line 95
def to_s
  "#{self.class.name.split('::').last || ''} #{@attributes}"
end

Private Instance Methods

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