Class: Rooftop::ResourceLinks::Link
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- Rooftop::ResourceLinks::Link
- Defined in:
- lib/rooftop/resource_links/link.rb
Instance Attribute Summary collapse
-
#link_type ⇒ Object
Returns the value of attribute link_type.
Instance Method Summary collapse
-
#initialize(link_type, args, klass = nil) ⇒ Link
constructor
A new instance of Link.
- #marshal_dump ⇒ Object
- #marshal_load(array) ⇒ Object
- #resolve(klass = nil) ⇒ Object
Constructor Details
#initialize(link_type, args, klass = nil) ⇒ Link
Returns a new instance of Link.
6 7 8 9 10 11 12 |
# File 'lib/rooftop/resource_links/link.rb', line 6 def initialize(link_type,args, klass=nil) @link_type = link_type @mapped_class = klass.try(:resource_link_mapping).try(:[],@link_type) @klass = klass super(args) end |
Instance Attribute Details
#link_type ⇒ Object
Returns the value of attribute link_type.
4 5 6 |
# File 'lib/rooftop/resource_links/link.rb', line 4 def link_type @link_type end |
Instance Method Details
#marshal_dump ⇒ Object
14 15 16 |
# File 'lib/rooftop/resource_links/link.rb', line 14 def marshal_dump [@link_type, @table, @klass] end |
#marshal_load(array) ⇒ Object
18 19 20 21 22 |
# File 'lib/rooftop/resource_links/link.rb', line 18 def marshal_load(array) @link_type = array[0] @table = array[1] @klass = array[2] end |
#resolve(klass = nil) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rooftop/resource_links/link.rb', line 24 def resolve(klass=nil) # We need to figure out what we're going to instantiate. If it's in the resource link mapping, use that. If not, try the klass passed into the resolve() method. Failing that, make an attempt to constantize something; otherwise we're going to have to raise @mapped_class ||= klass || @link_type.camelize.classify.constantize if @mapped_class # If this link has an ID, we can call find() on the class if respond_to?(:id) return @mapped_class.send(:find, id) else # TODO this is a fudge to get around some hrefs which don't have /wp/v2 in them unless href =~ /wp\/v2/ href.gsub!('wp-json','wp-json/wp/v2') end # otherwise we're going to have make a call to the link's href. result = @mapped_class.get(href) result.run_callbacks(:find) return result end else raise Rooftop::ResourceLinks::UnresolvableLinkError, "Couldn't resolve a link of type #{@link_type}. Try passing the class you want to resolve to." end end |