Sha256: de3ad12189e2005dfc1b2d1b761048e99752a820db3aca28c00fa8cecde04a74

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 KB

Contents

module Rooftop
  module ResourceLinks
    class Link < ::OpenStruct
      attr_accessor :link_type
      def initialize(link_type,args, klass=nil)
        @link_type = link_type
        @mapped_class = klass.try(:resource_link_mapping).try(:[],@link_type)
        super(args)
      end

      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
            # 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
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rooftop-0.1.2 lib/rooftop/resource_links/link.rb
rooftop-0.1.1 lib/rooftop/resource_links/link.rb
rooftop-0.0.7.4 lib/rooftop/resource_links/link.rb