Module: Balanced::Resource::ClassMethods
- Defined in:
- lib/balanced/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
286 287 288 289 |
# File 'lib/balanced/resources/resource.rb', line 286 def all( = {}) pager = paginate() pager.to_a end |
#collection_name ⇒ Object
205 206 207 |
# File 'lib/balanced/resources/resource.rb', line 205 def collection_name Utils.pluralize Utils.underscore(resource_name) end |
#collection_path ⇒ Object
209 210 211 212 213 214 215 216 217 218 |
# File 'lib/balanced/resources/resource.rb', line 209 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 Balanced.config[:version] == '1' # XXX: we should raise an exception if we get here. ["/v#{Balanced.config[:version]}", collection_name].compact.join '/' else collection_name end end |
#construct_from_response(payload) ⇒ Object
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 255 |
# File 'lib/balanced/resources/resource.rb', line 228 def construct_from_response(payload) payload = Balanced::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 raise 'This should basically never happen' end # > Singular resources are represented as JSON objects. However, # they are still wrapped inside an array: resource_body = value.first cls = Balanced.from_hypermedia_registry(key) instance = cls.new resource_body instance.hydrate(links, ) end instance end |
#fetch(*arguments) ⇒ Object Also known as: find
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/balanced/resources/resource.rb', line 257 def fetch(*arguments) if arguments.nil? || arguments.empty? || arguments[0].nil? || arguments[0].to_s.empty? # no symbol.empty? in 1.8.7 raise Balanced::NotFound end scope = arguments.slice!(0) = arguments.slice!(0) || {} case scope when :all then all() when :first then paginate().first else response = Balanced.get scope, construct_from_response response.body end end |
#href ⇒ Object
220 221 222 |
# File 'lib/balanced/resources/resource.rb', line 220 def href collection_path end |
#member_name ⇒ Object
224 225 226 |
# File 'lib/balanced/resources/resource.rb', line 224 def member_name Utils.underscore resource_name end |
#paginate(options = {}) ⇒ Object Also known as: scoped, where
279 280 281 |
# File 'lib/balanced/resources/resource.rb', line 279 def paginate( = {}) Balanced::Pager.new href, end |
#resource_name ⇒ Object
201 202 203 |
# File 'lib/balanced/resources/resource.rb', line 201 def resource_name Utils.demodulize name end |