Class: Canfig::Config

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

Direct Known Subclasses

OpenConfig

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/canfig/config.rb', line 108

def method_missing(meth, *args, &block)
  if meth.to_s.match(/=\Z/)
    opt = meth.to_s.gsub(/=/,'').to_sym
    return set(opt, args.first) if allowed?(opt)
  else
    return get(meth, *args, &block) if allowed?(meth)
  end

  super
end

Instance Method Details

#[](key) ⇒ Object



59
60
61
# File 'lib/canfig/config.rb', line 59

def [](key)
  get key
end

#[]=(key, val) ⇒ Object



50
51
52
# File 'lib/canfig/config.rb', line 50

def []=(key, val)
  set key, val
end

#allowed?(opt) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/canfig/config.rb', line 104

def allowed?(opt)
  @allowed.include?(opt)
end

#changedObject



67
68
69
# File 'lib/canfig/config.rb', line 67

def changed
  Hash[@allowed.map { |key| [key, [@saved_state[key], @state[key]]] if @saved_state[key] != @state[key] }.compact]
end

#changed?(key) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/canfig/config.rb', line 71

def changed?(key)
  changed.key?(key)
end

#clear(key) ⇒ Object



44
45
46
47
48
# File 'lib/canfig/config.rb', line 44

def clear(key)
  save_state! do
    @state[key] = nil
  end
end

#configure(argh = {}, file = nil, &block) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/canfig/config.rb', line 4

def configure(argh={}, file=nil, &block)
  save_state! do
    configure_with_file file unless file.nil?
    configure_with_args argh
    configure_with_block &block
  end
  self
end

#configure_with_args(argh) ⇒ Object



19
20
21
22
23
# File 'lib/canfig/config.rb', line 19

def configure_with_args(argh)
  save_state! do
    argh.symbolize_keys.each { |key,val| set(key, val) }
  end
end

#configure_with_block(&block) ⇒ Object



25
26
27
28
29
# File 'lib/canfig/config.rb', line 25

def configure_with_block(&block)
  save_state! do
    instance_eval &block if block_given?
  end
end

#configure_with_file(file) ⇒ Object



13
14
15
16
17
# File 'lib/canfig/config.rb', line 13

def configure_with_file(file)
  save_state! do
    configure_with_args(Canfig::YAML.new(file).load)
  end
end

#disable_state_saves!Object



96
97
98
# File 'lib/canfig/config.rb', line 96

def disable_state_saves!
  @save_state = false
end

#enable_state_saves!Object



92
93
94
# File 'lib/canfig/config.rb', line 92

def enable_state_saves!
  @save_state = true
end

#env(key, default = nil, &block) ⇒ Object



39
40
41
42
# File 'lib/canfig/config.rb', line 39

def env(key, default=nil, &block)
  val = ENV.fetch(key.to_s.underscore.upcase, default, &block)
  val && ENV.key?(val) ? env(val, default, &block) : val
end

#get(key, default = nil, &block) ⇒ Object

Raises:

  • (NoMethodError)


54
55
56
57
# File 'lib/canfig/config.rb', line 54

def get(key, default=nil, &block)
  raise NoMethodError, "undefined method `#{key.to_s}' for #{self.to_s}" unless allowed?(key)
  @state[key] ||= env(key, default, &block)
end

#save_state!(&block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/canfig/config.rb', line 75

def save_state!(&block)
  if save_state?
    @saved_state = to_h

    if block_given?
      disable_state_saves!
      begin
        yield
      ensure
        enable_state_saves!
      end
    end
  else
    yield if block_given?
  end
end

#save_state?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/canfig/config.rb', line 100

def save_state?
  @save_state
end

#set(key, val) ⇒ Object

Raises:

  • (NoMethodError)


31
32
33
34
35
36
37
# File 'lib/canfig/config.rb', line 31

def set(key, val)
  raise NoMethodError, "undefined method `#{key.to_s}=' for #{self.to_s}" unless allowed?(key)

  save_state! do
    @state[key] = val
  end
end

#to_hObject



63
64
65
# File 'lib/canfig/config.rb', line 63

def to_h
  Hash[@allowed.map { |key| [key, @state[key]] }]
end