lib/pmp/link.rb in pmp-0.1.2 vs lib/pmp/link.rb in pmp-0.1.3
- old
+ new
@@ -23,32 +23,23 @@
attr_accessor :parent
attr_accessor :params
- def initialize(parent=PMP::CollectionDocument.new, link={})
+ def initialize(parent=nil, link={})
super()
- self.parent = parent
+ self.parent = parent || PMP::CollectionDocument.new
self.params = link.delete('params') || {}
# puts "params: #{params.inspect}"
parse_attributes(link)
+ [:href, :href_template, :method].each{|m| self.send("#{m}=", nil) unless respond_to?(m)}
end
- def href
- self[:href]
- end
-
- def href_template
- self[:href_template]
- end
-
- def method
- self[:method]
- end
-
def attributes
- HashWithIndifferentAccess.new(marshal_dump)
+ attrs = HashWithIndifferentAccess.new(marshal_dump)
+ attrs.delete(attrs[:href_template].blank? ? :href_template : :href)
+ attrs
end
def where(params={})
self.class.new(parent, attributes.merge({'params'=>params}))
end
@@ -64,21 +55,23 @@
end
def retrieve
# puts "retrieve method: #{method}"
# puts "retrieve url: #{url}"
- parent.request((method || 'get').to_sym, url)
+ # response = parent.request((method || 'get').to_sym, url)
+ PMP::CollectionDocument.new(parent.options.merge(href: url))
end
def method_missing(method, *args)
# puts "mm: #{method}"
# this is a method the link supports, call the link
# if this is an assignment, assign to the link
# if you want to assign to a linked doc(s), need to retrieve first
- if self.respond_to?(method)
- self.send(method, *args)
- elsif method.to_s.last == '='
+ method_last = method.to_s.last
+ if method_last == '='
super
+ elsif self.respond_to?(method)
+ self.send(method, *args)
else
# puts "mm retrieve and send: #{method}"
self.retrieve.send(method, *args)
end
end