Class: Sfn::ResourceCollection
- Inherits:
-
HasSatisfaction
- Object
- HasSatisfaction
- Sfn::ResourceCollection
- Includes:
- Util
- Defined in:
- lib/satisfaction/resource.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Attributes inherited from HasSatisfaction
Instance Method Summary collapse
- #[](*key) ⇒ Object
- #get(id, options = {}) ⇒ Object
-
#initialize(klass, satisfaction, path) ⇒ ResourceCollection
constructor
A new instance of ResourceCollection.
- #page(number, options = {}) ⇒ Object
- #post(attrs) ⇒ Object
Methods included from Util
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
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
69 70 71 |
# File 'lib/satisfaction/resource.rb', line 69 def klass @klass end |
#path ⇒ Object (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) = key. case key.length when 1 get(key, ) when 2 page(key.first, .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 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, ={}) Sfn::Page.new(self, number, ) 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 |