Class: Tamashii::Config

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

Constant Summary collapse

SHARED_CONFIG =
%i(auth_type token log_file log_level token env).freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



24
25
26
27
# File 'lib/tamashii/config.rb', line 24

def initialize
  @accept_config = Set.new(SHARED_CONFIG)
  (self.class.default_config || {}).each { |name, value| register(name, value) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



29
30
31
32
33
34
35
# File 'lib/tamashii/config.rb', line 29

def method_missing(name, *args, &block)
  return unless accept?(name) || accept?(name[0..-2])
  return register(name[0..-2], args.first) if name[-1..-1] == "="
  return register(name, args.first) if args.size > 0
  return register(name, yield(self)) if block_given?
  self[name]
end

Class Attribute Details

.default_configObject

Returns the value of attribute default_config.



8
9
10
# File 'lib/tamashii/config.rb', line 8

def default_config
  @default_config
end

Class Method Details

.[](name) ⇒ Object



19
20
21
# File 'lib/tamashii/config.rb', line 19

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

.method_missing(name, *args, &block) ⇒ Object



10
11
12
# File 'lib/tamashii/config.rb', line 10

def method_missing(name, *args, &block)
  (@instance ||= self.new).send(name, *args, &block)
end

.register(name, default = nil) ⇒ Object



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

def register(name, default = nil)
  self.default_config ||= {}
  self.default_config[name.to_sym] = default
end

Instance Method Details

#accept?(key) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/tamashii/config.rb', line 42

def accept?(key)
  @accept_config.include?(key.to_sym)
end

#env(env = nil) ⇒ Object



46
47
48
49
# File 'lib/tamashii/config.rb', line 46

def env(env = nil)
  return Environment.new(self[:env]) if env.nil?
  self[:env] = env.to_s
end

#env=(env) ⇒ Object



51
52
53
# File 'lib/tamashii/config.rb', line 51

def env=(env)
  self[:env] = env.to_s
end

#register(key, default = nil) ⇒ Object



37
38
39
40
# File 'lib/tamashii/config.rb', line 37

def register(key, default = nil)
  @accept_config.add(key.to_sym)
  self[key.to_sym] = default if default
end