Sha256: cd286d2f03b6de386a6b41922fb36dfa507ab9d92a6c1cd1559fd9633e3e86cd

Contents?: true

Size: 951 Bytes

Versions: 6

Compression:

Stored size: 951 Bytes

Contents

module Rooftop
  module Nested
    
    def self.included(base)
      @nested_classes ||= []
      @nested_classes << base unless @nested_classes.include?(base)
    end

    def self.nested_classes
      @nested_classes
    end

    def root
      ancestors.last || resource_links.find_by(link_type: 'self').first
    end

    def ancestors
      if respond_to?(:resource_links)
        resource_links.find_by(link_type: "#{Rooftop::ResourceLinks::CUSTOM_LINK_RELATION_BASE}/ancestors")
      else
        []
      end
    end

    def children
      if respond_to?(:resource_links)
        resource_links.find_by(link_type: "#{Rooftop::ResourceLinks::CUSTOM_LINK_RELATION_BASE}/children")
      else
        []
      end
    end

    def parent
      if respond_to?(:resource_links) && resource_links
        ancestors.first
      end
    end

    def siblings
      self.class.find(parent.id).children.reject! {|c| c.id == self.id}
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rooftop-0.1.4.1 lib/rooftop/nested.rb
rooftop-0.1.4 lib/rooftop/nested.rb
rooftop-0.1.3 lib/rooftop/nested.rb
rooftop-0.1.2 lib/rooftop/nested.rb
rooftop-0.1.1 lib/rooftop/nested.rb
rooftop-0.0.7.4 lib/rooftop/nested.rb