Sha256: d71ec056c29a5b452fdffc13443177c12f8346bb83f1f6133373b60bd6d43521

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

module Arrest
  class RestChild < AbstractResource
    attr_accessor :parent
    def initialize parent, h
      super h
      @parent = parent
    end

    class << self
      # class method to generate path to a child resource of aonther resource
      def resource_path_for parent
        "#{parent.location}/#{self.resource_name}"
      end

      def build parent, hash
        self.new parent, hash
      end


      def all_for parent
        raise "Parent has no id yet" unless parent.id
        body_root(source().get self.resource_path_for(parent)).map do |h|
          self.build(parent, h)
        end
      end

      def find_for parent,id
        r = source().get "#{self.resource_path_for(parent)}/#{id}"
        body = body_root(r)
        self.build body
      end

    end

    # instance method to generate path to a child resource of another resource
    def resource_path
      self.class.resource_path_for @parent
    end

    # unique url for one instance of this class
    def location
      "#{self.class.resource_path}/#{self.id.to_s}"
    end


  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
arrest-0.0.4 lib/arrest/rest_child.rb
arrest-0.0.3 lib/arrest/rest_child.rb
arrest-0.0.2 lib/arrest/rest_child.rb