Module: Finix::Resource

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary

Attributes included from HalResource

#attributes, #hyperlinks

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HalResource

#load_page_from_response!, #method_missing

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Finix::HalResource

Class Method Details

.included(base) ⇒ Object



99
100
101
# File 'lib/finix/resources/resource.rb', line 99

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#copy_from(other) ⇒ Object



89
90
91
92
93
# 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) ⇒ Object Also known as: find



37
38
39
# File 'lib/finix/resources/resource.rb', line 37

def fetch(*arguments)
  self.class.find *arguments
end

#hydrate(links) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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

#initialize(*args) ⇒ Object



10
11
12
13
14
15
16
17
# 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

#refresh(the_response = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# 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

#sanitizeObject



64
65
66
67
68
69
70
# 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) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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_sObject



95
96
97
# File 'lib/finix/resources/resource.rb', line 95

def to_s
  "#{self.class.name.split('::').last || ''} #{@attributes}"
end