Class: ConfUtils::Props

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

Constant Summary collapse

VERSION =

remove version; depreciated api/constant; use ConfUtils::VERSION

ConfUtils::VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, path, parent = nil) ⇒ Props

Returns a new instance of Props.



26
27
28
29
30
# File 'lib/props/props.rb', line 26

def initialize( hash, path, parent=nil)
  @hash   = hash
  @path   = path
  @parent = parent
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.load_file(path, parent = nil) ⇒ Object



13
14
15
16
# File 'lib/props/props.rb', line 13

def self.load_file( path, parent=nil )
  h = YAML.load_file( path )
  Props.new( h, path, parent )
end

.load_file_with_erb(path, binding, parent = nil) ⇒ Object

todo: use TOP_LEVEL_BINDING for binding default?



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

def self.load_file_with_erb( path, binding, parent=nil )  # run through erb first
  text = ERB.new( File.read( path ) ).result( binding )
  h = YAML.load( text )
  Props.new( h, path, parent )
end

Instance Method Details

#[](key) ⇒ Object



48
# File 'lib/props/props.rb', line 48

def [](key)  get( key );  end

#dumpObject

for debugging



32
33
34
35
# File 'lib/props/props.rb', line 32

def dump   # for debugging
  puts "dump of >#{@path}<:"
  pp @hash
end

#fetch(key, default) ⇒ Object



38
39
40
41
# File 'lib/props/props.rb', line 38

def fetch(key, default)
  value = get( key )
  value.nil? ? default : value
end

#fetch_from_section(section, key, default) ⇒ Object



43
44
45
46
# File 'lib/props/props.rb', line 43

def fetch_from_section(section, key, default)
  value = get_from_section( section, key )
  value.nil? ? default : value
end

#get(key) ⇒ Object



50
51
52
53
54
# File 'lib/props/props.rb', line 50

def get( key )
  value = @hash.fetch( key.to_s, nil )
  # if not found try lookup in parent hash
  (value.nil? && parent) ? parent.get(key) : value
end

#get_from_section(section, key) ⇒ Object



56
57
58
59
60
# File 'lib/props/props.rb', line 56

def get_from_section( section, key )
  value = @hash.fetch( section.to_s, {} ).fetch( key.to_s, nil )
  # if not found try lookup in parent hash
  (value.nil? && parent) ? parent.get_from_section(section,key) : value
end