Class: Sfn::ResourceCollection

Inherits:
HasSatisfaction show all
Includes:
Util
Defined in:
lib/satisfaction/resource.rb

Instance Attribute Summary collapse

Attributes inherited from HasSatisfaction

#satisfaction

Instance Method Summary collapse

Methods included from Util

#requestify

Constructor Details

#initialize(klass, satisfaction, path) ⇒ ResourceCollection

Returns a new instance of ResourceCollection.



73
74
75
76
77
# File 'lib/satisfaction/resource.rb', line 73

def initialize(klass, satisfaction, path)
  super satisfaction
  @klass = klass
  @path = path
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



69
70
71
# File 'lib/satisfaction/resource.rb', line 69

def klass
  @klass
end

#pathObject (readonly)

Returns the value of attribute path.



70
71
72
# File 'lib/satisfaction/resource.rb', line 70

def path
  @path
end

Instance Method Details

#[](*key) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/satisfaction/resource.rb', line 105

def [](*key)
  options = key.extract_options!
  case key.length
  when 1
    get(key, options)
  when 2
    page(key.first, options.merge(:limit => key.last))
  else
    raise ArgumentError, "Invalid Array arguement, only use 2-element array: :first is the page number, :last is the page size"
  end
end

#get(id, options = {}) ⇒ Object



83
84
85
86
87
88
# File 'lib/satisfaction/resource.rb', line 83

def get(id, options={})
  #options currently ignored
  satisfaction.identity_map.get_record(klass, id) do
    klass.new(id, satisfaction)
  end
end

#page(number, options = {}) ⇒ Object



79
80
81
# File 'lib/satisfaction/resource.rb', line 79

def page(number, options={})
  Sfn::Page.new(self, number, options)
end

#post(attrs) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/satisfaction/resource.rb', line 90

def post(attrs)
  params = requestify(attrs, klass.name.demodulize.underscore)
  result = satisfaction.post("#{path}.json", params)
  
  if result.first == :ok
    json = JSON.parse(result.last)
    id = json["id"]
    obj = klass.new(id, satisfaction)
    obj.attributes = json
    obj
  else
    result
  end
end