Module: Datmachine::Resource::ClassMethods
- Defined in:
- lib/datmachine/resources/resource.rb
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #collection_name ⇒ Object
- #collection_path ⇒ Object
- #construct_from_response(payload) ⇒ Object
- #fetch(*arguments) ⇒ Object (also: #find)
- #href ⇒ Object
- #member_name ⇒ Object
- #paginate(options = {}) ⇒ Object (also: #scoped, #where)
- #resource_name ⇒ Object
Instance Method Details
#all(options = {}) ⇒ Object
285 286 287 288 |
# File 'lib/datmachine/resources/resource.rb', line 285 def all( = {}) pager = paginate() pager.to_a end |
#collection_name ⇒ Object
201 202 203 |
# File 'lib/datmachine/resources/resource.rb', line 201 def collection_name Utils.pluralize Utils.underscore(resource_name) end |
#collection_path ⇒ Object
205 206 207 208 209 210 211 212 213 214 |
# File 'lib/datmachine/resources/resource.rb', line 205 def collection_path # this is to just support version1 stuff, but in reality, if we get here # we should throw an exception since we do not want to use v1 ever. if Datmachine.config[:version] == '1' # XXX: we should raise an exception if we get here. ["/v#{Datmachine.config[:version]}", collection_name].compact.join '/' else collection_name end end |
#construct_from_response(payload) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/datmachine/resources/resource.rb', line 224 def construct_from_response(payload) payload = Datmachine::Utils.indifferent_read_access payload links = payload.delete('links') || {} = payload.delete('meta') || {} if payload.length > 1 raise 'NOT SUPPORTED YET' end instance = nil # the remaining keys here are just hypermedia resources payload.each do |key, value| # > If the length of an array at a resource key is greater than one, # the value represents a list of documents. if value.length > 1 # key is a collection, this should never happen resource_body = value else resource_body = value.first end # > Singular resources are represented as JSON objects. However, # they are still wrapped inside an array: #resource_body = value.first cls = ("Datmachine::" + Datmachine::Utils.classify(key)).constantize instance = cls.new resource_body instance.hydrate(links, ) end instance end |
#fetch(*arguments) ⇒ Object Also known as: find
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/datmachine/resources/resource.rb', line 256 def fetch(*arguments) if arguments.nil? || arguments.empty? || arguments[0].nil? || arguments[0].empty? end scope = arguments.slice!(0) = arguments.slice!(0) || {} case scope when :all then all() when :first then paginate().first else response = Datmachine.get scope, construct_from_response response.body end end |
#href ⇒ Object
216 217 218 |
# File 'lib/datmachine/resources/resource.rb', line 216 def href collection_path end |
#member_name ⇒ Object
220 221 222 |
# File 'lib/datmachine/resources/resource.rb', line 220 def member_name Utils.underscore resource_name end |
#paginate(options = {}) ⇒ Object Also known as: scoped, where
278 279 280 |
# File 'lib/datmachine/resources/resource.rb', line 278 def paginate( = {}) Datmachine::Pager.new href, end |
#resource_name ⇒ Object
197 198 199 |
# File 'lib/datmachine/resources/resource.rb', line 197 def resource_name Utils.demodulize name end |