Class: Termtter::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/termtter/config.rb

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



5
6
7
# File 'lib/termtter/config.rb', line 5

def initialize
  @store = Hash.new(:undefined)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/termtter/config.rb', line 34

def method_missing(name, *args)
  case name.to_s
  when /(.*)=$/
    __assign__($1.to_sym, args.first)
  else
    __refer__(name.to_sym)
  end
end

Instance Method Details

#__assign__(name, value) ⇒ Object

__assign__

Symbol -> a -> IO ()



44
45
46
# File 'lib/termtter/config.rb', line 44

def __assign__(name, value)
  @store[name] = value
end

#__clear__Object



57
58
59
# File 'lib/termtter/config.rb', line 57

def __clear__
  @store.clear
end

#__refer__(name) ⇒ Object

__refer__

Symbol -> IO a



49
50
51
# File 'lib/termtter/config.rb', line 49

def __refer__(name)
  @store[name] == :undefined ? @store[name] = Termtter::Config.new : @store[name]
end

#__values__Object



53
54
55
# File 'lib/termtter/config.rb', line 53

def __values__
  @store.dup
end

#empty?Boolean

empty?

Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/termtter/config.rb', line 30

def empty?
  @store.empty?
end

#inspectObject



9
10
11
# File 'lib/termtter/config.rb', line 9

def inspect
  @store.inspect
end

#set_default(name, value) ⇒ Object

set_default

(Symbol | String) -> a -> IO ()



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/termtter/config.rb', line 14

def set_default(name, value)
  match_p, init, last = *name.to_s.match(/^(.+)\.([^\.]+)$/)
  if match_p
    tmp = eval(init)
    if tmp.__refer__(last.to_sym).empty?
      tmp.__assign__(last.to_sym, value)
    end
  else
    current_value = __refer__(name.to_sym)
    if current_value.kind_of?(self.class) && current_value.empty?
      __assign__(name.to_sym, value)
    end
  end
end