Skip to content
Browse files

Move class-level methods into separate modules

This is so that class methods are not also instance methods and
vice-versa
  • Loading branch information...
1 parent d9644e8 commit e8256b718d646ba48ac55d814d35a87cbaca373d @thomsbg thomsbg committed with Aug 9, 2012
Showing with 137 additions and 127 deletions.
  1. +2 −2 lib/her/model.rb
  2. +88 −80 lib/her/model/orm.rb
  3. +47 −45 lib/her/model/paths.rb
View
4 lib/her/model.rb
@@ -29,10 +29,10 @@ module Model
included do
extend Her::Model::Base
extend Her::Model::HTTP
- extend Her::Model::ORM
+ extend Her::Model::ORM::ClassMethods
extend Her::Model::Relationships
extend Her::Model::Hooks
- extend Her::Model::Paths
+ extend Her::Model::Paths::ClassMethods
# Define default settings
base_path = self.name.split("::").last.underscore.pluralize
View
168 lib/her/model/orm.rb
@@ -48,13 +48,6 @@ def id # {{{
@data[:id] || super
end # }}}
- # Initialize a collection of resources with raw data from an HTTP request
- #
- # @param [Array] parsed_data
- def new_collection(parsed_data) # {{{
- Her::Model::ORM.initialize_collection(self, parsed_data)
- end # }}}
-
# Return `true` if a resource was not saved yet
def new? # {{{
!@data.include?(:id)
@@ -70,68 +63,6 @@ def invalid? # {{{
@errors.any?
end # }}}
- # Fetch specific resource(s) by their ID
- #
- # @example
- # @user = User.find(1)
- # # Fetched via GET "/users/1"
- #
- # @example
- # @users = User.find([1, 2])
- # # Fetched via GET "/users/1" and GET "/users/2"
- def find(*ids) # {{{
- params = ids.last.is_a?(Hash) ? ids.pop : {}
- results = ids.flatten.compact.uniq.map do |id|
- request(params.merge(:_method => :get, :_path => "#{build_request_path(params.merge(:id => id))}")) do |parsed_data|
- new(parsed_data[:data])
- end
- end
- if ids.length > 1 || ids.first.kind_of?(Array)
- results
- else
- results.first
- end
- end # }}}
-
- # Fetch a collection of resources
- #
- # @example
- # @users = User.all
- # # Fetched via GET "/users"
- def all(params={}) # {{{
- request(params.merge(:_method => :get, :_path => "#{build_request_path(params)}")) do |parsed_data|
- new_collection(parsed_data)
- end
- end # }}}
-
- # Create a resource and return it
- #
- # @example
- # @user = User.create({ :fullname => "Tobias Fünke" })
- # # Called via POST "/users/1"
- def create(params={}) # {{{
- resource = new(params)
- wrap_in_hooks(resource, :create, :save) do |resource, klass|
- params = resource.to_params
- request(params.merge(:_method => :post, :_path => "#{build_request_path(params)}")) do |parsed_data|
- resource.data = parsed_data[:data]
- resource.metadata = parsed_data[:metadata]
- resource.errors = parsed_data[:errors]
- end
- end
- resource
- end # }}}
-
- # Save an existing resource and return it
- #
- # @example
- # @user = User.save_existing(1, { :fullname => "Tobias Fünke" })
- # # Called via PUT "/users/1"
- def save_existing(id, params) # {{{
- resource = new(params.merge(:id => id))
- resource.save
- end # }}}
-
# Save a resource
#
# @example Save a resource after fetching it
@@ -185,17 +116,6 @@ def destroy # {{{
self
end # }}}
- # Destroy an existing resource
- #
- # @example
- # User.destroy_existing(1)
- # # Called via DELETE "/users/1"
- def destroy_existing(id, params={}) # {{{
- request(params.merge(:_method => :delete, :_path => "#{build_request_path(params.merge(:id => id))}")) do |parsed_data|
- new(parsed_data[:data])
- end
- end # }}}
-
# Convert into a hash of request parameters
#
# @example
@@ -204,6 +124,94 @@ def destroy_existing(id, params={}) # {{{
def to_params # {{{
@data.dup
end # }}}
+
+ module ClassMethods
+ # Initialize a collection of resources with raw data from an HTTP request
+ #
+ # @param [Array] parsed_data
+ def new_collection(parsed_data) # {{{
+ Her::Model::ORM.initialize_collection(self, parsed_data)
+ end # }}}
+
+ # Fetch specific resource(s) by their ID
+ #
+ # @example
+ # @user = User.find(1)
+ # # Fetched via GET "/users/1"
+ #
+ # @example
+ # @users = User.find(1, 2)
+ # # Fetched via GET "/users/1" and GET "/users/2"
+ def find(*ids) # {{{
+ params = ids.last.is_a?(Hash) ? ids.pop : {}
+ results = ids.map do |id|
+ request_params = params.merge(
+ :_method => :get,
+ :_path => "#{build_request_path(params.merge(:id => id))}"
+ )
+ request(request_params) do |parsed_data|
+ new(parsed_data[:data])
+ end
+ end
+ if ids.length == 1 || ids.first.kind_of?(Array)
+ results.first
+ else
+ results
+ end
+ end # }}}
+
+ # Fetch a collection of resources
+ #
+ # @example
+ # @users = User.all
+ # # Fetched via GET "/users"
+ def all(params={}) # {{{
+ request(params.merge(:_method => :get, :_path => "#{build_request_path(params)}")) do |parsed_data|
+ new_collection(parsed_data)
+ end
+ end # }}}
+
+ # Create a resource and return it
+ #
+ # @example
+ # @user = User.create({ :fullname => "Tobias Fünke" })
+ # # Called via POST "/users/1"
+ def create(params={}) # {{{
+ resource = new(params)
+ wrap_in_hooks(resource, :create, :save) do |resource, klass|
+ params = resource.to_params
+ request(params.merge(:_method => :post, :_path => "#{build_request_path(params)}")) do |parsed_data|
+ resource.instance_eval do
+ @data = parsed_data[:data]
+ @metadata = parsed_data[:metadata]
+ @errors = parsed_data[:errors]
+ end
+ end
+ end
+ resource
+ end # }}}
+
+ # Save an existing resource and return it
+ #
+ # @example
+ # @user = User.save_existing(1, { :fullname => "Tobias Fünke" })
+ # # Called via PUT "/users/1"
+ def save_existing(id, params) # {{{
+ resource = new(params.merge(:id => id))
+ resource.save
+ end # }}}
+
+ # Destroy an existing resource
+ #
+ # @example
+ # User.destroy_existing(1)
+ # # Called via DELETE "/users/1"
+ def destroy_existing(id, params={}) # {{{
+ request(params.merge(:_method => :delete, :_path => "#{build_request_path(params.merge(:id => id))}")) do |parsed_data|
+ new(parsed_data[:data])
+ end
+ end # }}}
+ end
end
end
end
View
92 lib/her/model/paths.rb
@@ -1,51 +1,6 @@
module Her
module Model
module Paths
- # Defines a custom collection path for the resource
- #
- # @example
- # class User
- # include Her::Model
- # collection_path "/users"
- # end
- def collection_path(path=nil) # {{{
- return @her_collection_path unless path
- @her_resource_path = "#{path}/:id"
- @her_collection_path = path
- end # }}}
-
- # Defines a custom resource path for the resource
- #
- # @example
- # class User
- # include Her::Model
- # resource_path "/users/:id"
- # end
- def resource_path(path=nil) # {{{
- return @her_resource_path unless path
- @her_resource_path = path
- end # }}}
-
- # Return a custom path based on the collection path and variable parameters
- #
- # @example
- # class User
- # include Her::Model
- # collection_path "/utilisateurs"
- # end
- #
- # User.all # Fetched via GET /utilisateurs
- def build_request_path(path=nil, parameters={}) # {{{
- unless path.is_a?(String)
- parameters = path || {}
- path = parameters.include?(:id) ? @her_resource_path : @her_collection_path
- end
- path.gsub(/:([\w_]+)/) do
- # Look for :key or :_key, otherwise raise an exception
- parameters[$1.to_sym] || parameters["_#{$1}".to_sym] || raise(Her::Errors::PathError.new("Missing :_#{$1} parameter to build the request path (#{path})."))
- end
- end # }}}
-
# Return a path based on the collection path and a resource data
#
# @example
@@ -58,6 +13,53 @@ def build_request_path(path=nil, parameters={}) # {{{
def request_path # {{{
self.class.build_request_path(@data)
end # }}}
+
+ module ClassMethods
+ # Defines a custom collection path for the resource
+ #
+ # @example
+ # class User
+ # include Her::Model
+ # collection_path "/users"
+ # end
+ def collection_path(path=nil) # {{{
+ return @her_collection_path unless path
+ @her_resource_path = "#{path}/:id"
+ @her_collection_path = path
+ end # }}}
+
+ # Defines a custom resource path for the resource
+ #
+ # @example
+ # class User
+ # include Her::Model
+ # resource_path "/users/:id"
+ # end
+ def resource_path(path=nil) # {{{
+ return @her_resource_path unless path
+ @her_resource_path = path
+ end # }}}
+
+ # Return a custom path based on the collection path and variable parameters
+ #
+ # @example
+ # class User
+ # include Her::Model
+ # collection_path "/utilisateurs"
+ # end
+ #
+ # User.all # Fetched via GET /utilisateurs
+ def build_request_path(path=nil, parameters={}) # {{{
+ unless path.is_a?(String)
+ parameters = path || {}
+ path = parameters.include?(:id) ? @her_resource_path : @her_collection_path
+ end
+ path.gsub(/:([\w_]+)/) do
+ # Look for :key or :_key, otherwise raise an exception
+ parameters[$1.to_sym] || parameters["_#{$1}".to_sym] || raise(Her::Errors::PathError.new("Missing :_#{$1} parameter to build the request path (#{path})."))
+ end
+ end # }}}
+ end
end
end
end

0 comments on commit e8256b7

Please sign in to comment.
Something went wrong with that request. Please try again.