Sha256: e3b4d58d6f7a20aa2a3c98fb011c97c6003df914177796401be874cbb6b5a031

Contents?: true

Size: 898 Bytes

Versions: 12

Compression:

Stored size: 898 Bytes

Contents

module Lookbook
  class Collection
    attr_reader :path

    def initialize(path = "")
      @path = path.delete_prefix("/").delete_suffix("/")
      @items = []
    end

    def id
      (@path || "root").underscore.tr("_", "-")
    end

    def name
      @path ? @path.split("/").last : "root"
    end

    def label
      name.titleize
    end

    def hierarchy_depth
      @path ? @path.split("/").size : 0
    end

    def items(sorted = true)
      sorted ? @items.sort_by(&:name) : @items
    end

    def add(item)
      if item.is_a?(String)
        item = Collection.new([@path, item].join("/"))
      end
      @items << item
      item
    end

    def get(name)
      name = name.underscore
      @items.find { |item| item.name.underscore == name }
    end

    def get_or_create(name)
      get(name).presence || add(name)
    end

    def type
      :collection
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
lookbook-0.3.3 lib/lookbook/collection.rb
lookbook-0.3.2 lib/lookbook/collection.rb
lookbook-0.3.1 lib/lookbook/collection.rb
lookbook-0.3.0 lib/lookbook/collection.rb
lookbook-0.3.0.beta.2 lib/lookbook/collection.rb
lookbook-0.3.0.beta.1 lib/lookbook/collection.rb
lookbook-0.3.0.beta.0 lib/lookbook/collection.rb
lookbook-0.2.4 lib/lookbook/collection.rb
lookbook-0.2.3 lib/lookbook/collection.rb
lookbook-0.2.2 lib/lookbook/collection.rb
lookbook-0.2.1 lib/lookbook/collection.rb
lookbook-0.2.0 lib/lookbook/collection.rb