Permalink
Browse files

Make collection_path and resource_path inheritable

Makes it so subclasses automatically inherit a copy of their
superclass' collection_path and resource_path metadata. This allows
subclasses of a class including Her::Model to work as expected.
  • Loading branch information...
thomsbg authored and remiprev committed Aug 14, 2012
1 parent c1c2a25 commit d1f443a46ba2cda01380fc476e2096ddd8cb3e7b
Showing with 10 additions and 1 deletion.
  1. +10 −1 lib/her/model/paths.rb
View
@@ -23,6 +23,10 @@ module ClassMethods
# collection_path "/users"
# end
def collection_path(path=nil) # {{{
+ @her_collection_path ||= begin
+ superclass.collection_path.dup if superclass.respond_to?(:collection_path)
+ end
+
return @her_collection_path unless path
@her_resource_path = "#{path}/:id"
@her_collection_path = path
@@ -36,6 +40,10 @@ def collection_path(path=nil) # {{{
# resource_path "/users/:id"
# end
def resource_path(path=nil) # {{{
+ @her_resource_path ||= begin
+ superclass.resource_path.dup if superclass.respond_to?(:resource_path)
+ end
+
return @her_resource_path unless path
@her_resource_path = path
end # }}}
@@ -52,8 +60,9 @@ def resource_path(path=nil) # {{{
def build_request_path(path=nil, parameters={}) # {{{
unless path.is_a?(String)
parameters = path || {}
- path = parameters.include?(:id) ? @her_resource_path : @her_collection_path
+ path = parameters.include?(:id) ? resource_path : collection_path
end
+
path.gsub(/:([\w_]+)/) do
# Look for :key or :_key, otherwise raise an exception
parameters.delete($1.to_sym) || parameters.delete("_#{$1}".to_sym) || raise(Her::Errors::PathError.new("Missing :_#{$1} parameter to build the request path (#{path})."))

0 comments on commit d1f443a

Please sign in to comment.