Class: Egi::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/egi/env.rb

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Env

Returns a new instance of Env.



3
4
5
6
# File 'lib/egi/env.rb', line 3

def initialize(name)
  @name    = name
  @default = {}
end

Instance Method Details

#[](name) ⇒ Object



30
31
32
# File 'lib/egi/env.rb', line 30

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

#group(name = nil, &block) ⇒ Object



18
19
20
# File 'lib/egi/env.rb', line 18

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

#item(name, hash = {}) ⇒ Object



26
27
28
# File 'lib/egi/env.rb', line 26

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

#itemsObject



22
23
24
# File 'lib/egi/env.rb', line 22

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

#merge!(other) ⇒ Object



12
13
14
15
16
# File 'lib/egi/env.rb', line 12

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

#set(default = {}) ⇒ Object



8
9
10
# File 'lib/egi/env.rb', line 8

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

#tagged(name) ⇒ Object



38
39
40
41
# File 'lib/egi/env.rb', line 38

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

#tagsObject



34
35
36
# File 'lib/egi/env.rb', line 34

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