Sha256: b89be90f6f83f0dc7710c9a12661af760d4368b3379e4adb92775c36c4b628fb

Contents?: true

Size: 814 Bytes

Versions: 2

Compression:

Stored size: 814 Bytes

Contents

class HyperResource
  class Links < Hash
    attr_accessor :resource

    def initialize(resource=nil)
     self.resource = resource || HyperResource.new
    end

    # Initialize links from a HAL response.
    def init_from_hal(hal_resp)
      return unless hal_resp['_links']
      hal_resp['_links'].each do |rel, link_spec|
        self[rel] = new_link_from_spec(link_spec)
        create_methods_for_link_rel(rel) unless self.respond_to?(rel.to_sym)
      end
    end

  protected

    def new_link_from_spec(link_spec) # :nodoc:
      HyperResource::Link.new(resource, link_spec)
    end

    def create_methods_for_link_rel(rel) # :nodoc:
      link = self[rel]
      define_singleton_method(rel.to_sym) do |*args|
        return link if args.empty?
        link.where(*args)
      end
    end

  end
end


Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hyperresource-0.1.3 lib/hyper_resource/links.rb
hyperresource-0.1.2 lib/hyper_resource/links.rb