Sha256: 21ad98cc1aaadc759a55248277b730a3b762276fe2bdfa33efed535c90c072a4

Contents?: true

Size: 963 Bytes

Versions: 1

Compression:

Stored size: 963 Bytes

Contents

module Egi
  class Env
    def initialize(name)
      @name    = name
      @default = {}
    end

    def set(default = {})
      @default = default
    end

    def merge!(other)
      other = (other.is_a?(Env) ? other.items : other)
      # because items has default proc and cannnot dump
      items.merge!(Marshal.load(Marshal.dump(Hash[other])))
    end

    def group(name = nil, &block)
      items.merge!(Class.new(Group).new(name).instance_eval(&block).items)
    end

    def items
      @items ||= Hash.new {|hash, key| hash[key] = Item[{ :name => key }.merge(@default)] }
    end

    def item(name, hash = {})
      items[name.to_sym].update(hash)
    end

    def [](name)
      items[name.to_sym]
    end

    def tags
      @tags ||= items.values.map {|item| item[:tags] || [] }.flatten.uniq
    end

    def tagged(name)
      name = name.to_sym
      tags.include?(name) ? items.select {|k, v| v[:tags].include?(name) } : nil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
egi-0.0.4 lib/egi/env.rb