Module: Resto::ClassMethods
- Defined in:
- lib/resto.rb
Instance Method Summary collapse
- #all(params = {}, request_path_options = {}, &block) ⇒ Object
- #base_response ⇒ Object
- #belongs_to(name) ⇒ Object
- #delete(id, request_path_options = {}, &block) ⇒ Object
- #fetch(params = {}, request_path_options = {}, &block) ⇒ Object
- #get(id, request_path_options = {}, &block) ⇒ Object
- #has_many(name, options) ⇒ Object
- #head(request_path_options = {}) ⇒ Object
- #inherited(sub_class) ⇒ Object
- #post(attributes, request_path_options = {}, &block) ⇒ Object
- #property(name, property, options = {}, &block) ⇒ Object
- #property_handler ⇒ Object
- #put(attributes, request_path_options = {}, &block) ⇒ Object
- #request ⇒ Object
- #resource_id ⇒ Object
- #resource_identifier(id) ⇒ Object
- #response(response) ⇒ Object
- #resto_request(&block) ⇒ Object
- #resto_response(&block) ⇒ Object
Instance Method Details
#all(params = {}, request_path_options = {}, &block) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/resto.rb', line 117 def all(params = {}, = {}, &block) req = @request.construct_path() block.call(req) if block_given? res = if params.keys.empty? req.get else req.params(params).get end response(res).to_collection end |
#base_response ⇒ Object
190 191 192 |
# File 'lib/resto.rb', line 190 def base_response Copy.response_base(@response) end |
#belongs_to(name) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/resto.rb', line 74 def belongs_to(name) method_definition = %Q{ def #{name}(reload = false) if reload @#{name} = #{name.to_s.capitalize}.get(#{name}_id) else @#{name} ||= #{name.to_s.capitalize}.get(#{name}_id) end end } class_eval(method_definition, __FILE__, __LINE__) end |
#delete(id, request_path_options = {}, &block) ⇒ Object
175 176 177 178 179 180 |
# File 'lib/resto.rb', line 175 def delete(id, = {}, &block) req = @request.construct_path() block.call(req) if block_given? response(req.append_path(id).delete).to_object end |
#fetch(params = {}, request_path_options = {}, &block) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/resto.rb', line 143 def fetch(params = {}, = {}, &block) req = @request.construct_path() block.call(req) if block_given? res = if params.keys.empty? req.get else req.params(params).get end response(res).to_object end |
#get(id, request_path_options = {}, &block) ⇒ Object
136 137 138 139 140 141 |
# File 'lib/resto.rb', line 136 def get(id, = {}, &block) req = @request.construct_path() block.call(req) if block_given? response(req.append_path(id).get).to_object end |
#has_many(name, options) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/resto.rb', line 50 def has_many(name, ) class_name = .fetch(:class_name) params = .fetch(:params, {}) params = params.map { |key, value| ":#{key} => #{value}" }.join(', ') relation = .fetch(:relation, {}) relation = relation.map { |key, value| ":#{key} => #{value}" }.join(', ') method_definition = %Q{ def #{name}(params = {}) params ||= {} raise ArgumentError unless params.is_a?(Hash) @#{name} ||= {} @#{name}[params] ||= #{class_name.to_s.capitalize}. all({#{params}}.update(params), {#{relation}}) end } class_eval(method_definition, __FILE__, __LINE__) end |
#head(request_path_options = {}) ⇒ Object
131 132 133 134 |
# File 'lib/resto.rb', line 131 def head( = {}) req = @request.construct_path() response(req.head) end |
#inherited(sub_class) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/resto.rb', line 26 def inherited(sub_class) sub_class.class_exec(self) do |parent_class| @request = parent_class.request @response = parent_class.base_response.klass(sub_class) end end |
#post(attributes, request_path_options = {}, &block) ⇒ Object
157 158 159 160 161 162 163 164 |
# File 'lib/resto.rb', line 157 def post(attributes, = {}, &block) req = @request.construct_path() block.call(req) if block_given? attributes.delete(resource_id) remote_attributes = property_handler.remote_attributes(attributes) response(req.body(remote_attributes).post).to_object end |
#property(name, property, options = {}, &block) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/resto.rb', line 89 def property(name, property, ={}, &block) property = Resto::Property.const_get(property.to_s).new(name, ) property.instance_exec(&block) if block_given? property_handler.add(property) attribute_methods = %Q{ def #{name} @attributes.get(:#{name}) end def #{name}_without_cast @attributes.get_without_cast(:#{name}) end def #{name}? @attributes.present?(:#{name}) end def #{name}=(value) @attributes.set(:#{name}, value) end } class_eval(attribute_methods, __FILE__, __LINE__) end |
#property_handler ⇒ Object
194 195 196 |
# File 'lib/resto.rb', line 194 def property_handler @property_handler ||= Property::Handler.new end |
#put(attributes, request_path_options = {}, &block) ⇒ Object
166 167 168 169 170 171 172 173 |
# File 'lib/resto.rb', line 166 def put(attributes, = {}, &block) req = @request.construct_path() block.call(req) if block_given? id = attributes.delete(resource_id) remote_attributes = property_handler.remote_attributes(attributes) response(req.append_path(id).body(remote_attributes).put).to_object end |
#request ⇒ Object
182 183 184 |
# File 'lib/resto.rb', line 182 def request Copy.request_base(@request) end |
#resource_id ⇒ Object
42 43 44 |
# File 'lib/resto.rb', line 42 def resource_id @resource_identifier end |
#resource_identifier(id) ⇒ Object
46 47 48 |
# File 'lib/resto.rb', line 46 def resource_identifier(id) @resource_identifier = id end |
#response(response) ⇒ Object
186 187 188 |
# File 'lib/resto.rb', line 186 def response(response) base_response.http_response(response) end |
#resto_request(&block) ⇒ Object
33 34 35 |
# File 'lib/resto.rb', line 33 def resto_request(&block) @request.instance_exec(&block) end |
#resto_response(&block) ⇒ Object
38 39 40 |
# File 'lib/resto.rb', line 38 def resto_response(&block) @response.instance_exec(&block) end |