Class: Yaconfig::Configuration

Inherits:
SymbolTable
  • Object
show all
Defined in:
lib/yaconfig.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil, &block) ⇒ Configuration

Setup @data. Accept an optional hash of config.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/yaconfig.rb', line 13

def initialize(data=nil, &block)
  # Base name of the program, minus .rb if any.
  @base_name = File::basename($0)
  @base_name = @base_name[0..-4] if @base_name =~ /\.rb$/

  # Off by default
  @verbose = false

  # List of loaded config files
  @configs = []

  # Install any passed data into the SymbolTable
  r = super(data)

  # Do a block style config, if any
  block.call(self) if block_given?

  # Return what super gave us.
  return r
end

Instance Attribute Details

#base_nameObject

Legacy module to recieve constants



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

def base_name
  @base_name
end

#verboseObject

Legacy module to recieve constants



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

def verbose
  @verbose
end

Instance Method Details

#configure(&block) ⇒ Object

Use a block-style configure setup.

config.configure do |c|

c.whatever = a_value

end



53
54
55
56
57
# File 'lib/yaconfig.rb', line 53

def configure(&block)
    raise "No block given to configure." if !block_given?

    block.call(self)
end

#configure_json(json_text) ⇒ Object

JSON support



66
67
68
# File 'lib/yaconfig.rb', line 66

def configure_json(json_text)
  self.merge!(JSON.parse(json_text))
end

#load_config(*args) ⇒ Object

Search a list of files, and load any that exist. Provides the object as ‘config’



61
62
63
# File 'lib/yaconfig.rb', line 61

def load_config(*args)
  args.flatten.each{ |f| load_config_file(f) }
end

#load_config_json(*args) ⇒ Object



74
75
76
77
78
# File 'lib/yaconfig.rb', line 74

def load_config_json(*args)
  args.flatten.each{ |f| 
    configure_json(File.new(f, 'r').read) if File.exists?(f) 
  }
end

#to_json_prettyObject



70
71
72
# File 'lib/yaconfig.rb', line 70

def to_json_pretty()
  JSON.pretty_generate(self)
end