Sha256: 050274e8a5fed753af19f1073f8e98c24b91d7ab5ebe8806329ba9de91aeba4c

Contents?: true

Size: 1.33 KB

Versions: 7

Compression:

Stored size: 1.33 KB

Contents

module PrintReleaf
  class Relation
    include Enumerable

    attr_reader :owner
    attr_reader :resource_class
    attr_reader :path
    attr_reader :actions

    def initialize(owner, resource_class, options={})
      @owner          = owner
      @resource_class = resource_class
      @path           = options[:path] || resource_class.uri
      @actions        = Set.new(options[:actions] || resource_class.actions)
      @actions.each do |action|
        extend Actions.const_get(action.to_s.capitalize)
      end
    end

    def uri
      Util.join_uri(owner.uri, path)
    end

    def new(*args)
      @resource_class.new(*args).tap do |resource|
        resource.owner = owner
      end
    end

    def related
      if respond_to?(:list)
        return list
      else
        raise "Relation not defined."
      end
    end

    def each
      return enum_for(:each) unless block_given?
      related.each do |resource|
        yield resource
      end
    end

    def first
      related.first
    end

    def last
      related.last
    end

    def count
      related.count
    end

    def length
      related.length
    end

    def to_s
      "#<#{self.class.name}(#{resource_class}>"
    end

    def inspect
      "#<#{self.class}(#{resource_class}) owner=#{owner.compact_inspect} path=#{path} actions=#{actions}>"
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
printreleaf-1.5.0 lib/printreleaf/relation.rb
printreleaf-1.3.2 lib/printreleaf/relation.rb
printreleaf-1.3.1 lib/printreleaf/relation.rb
printreleaf-1.3.0 lib/printreleaf/relation.rb
printreleaf-1.0.3 lib/printreleaf/relation.rb
printreleaf-1.0.2 lib/printreleaf/relation.rb
printreleaf-1.0.1 lib/printreleaf/relation.rb