Class: GemSuit::CLI::Config::Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_suit/cli/config/hash.rb

Instance Method Summary collapse

Constructor Details

#initialize(file, default = {}) ⇒ Hash

Returns a new instance of Hash.



8
9
10
11
# File 'lib/gem_suit/cli/config/hash.rb', line 8

def initialize(file, default = {})
  @filename = file
  @hash ||= ::Thor::CoreExt::HashWithIndifferentAccess.new File.exists?(@filename) ? YAML.load_file(@filename) : default
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



40
41
42
43
44
45
46
# File 'lib/gem_suit/cli/config/hash.rb', line 40

def method_missing(method, *args)
  if hash.respond_to?(method) || method.to_s =~ /^(\w+)\?$/
    hash.send method, *args
  else
    super
  end
end

Instance Method Details

#[](key) ⇒ Object



29
30
31
# File 'lib/gem_suit/cli/config/hash.rb', line 29

def [](key)
  hash[key]
end

#[]=(key, value) ⇒ Object



33
34
35
36
# File 'lib/gem_suit/cli/config/hash.rb', line 33

def []=(key, value)
  hash[key] = value
  dump_file
end

#dumpObject



13
14
15
# File 'lib/gem_suit/cli/config/hash.rb', line 13

def dump
  YAML.dump hash.inject({}){|h, (k, v)| h[k.to_sym] = v; h}
end

#to_strObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gem_suit/cli/config/hash.rb', line 17

def to_str
  hash.collect do |key, value|
    val = case value.class.name
          when "Array"
            value.join ","
          else
            value
          end
    "#{key}=#{val}"
  end.join "\n"
end