Class: Pisec::Settings

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_data = {}, args = {}) ⇒ Settings

Returns a new instance of Settings.



55
56
57
58
# File 'lib/pisec/settings.rb', line 55

def initialize( _data = {}, args = {} )
  @namespace = args[:namespace].to_s || ''
  load( _data )
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



54
55
56
# File 'lib/pisec/settings.rb', line 54

def data
  @data
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



54
55
56
# File 'lib/pisec/settings.rb', line 54

def namespace
  @namespace
end

Class Method Details

._open_file(file_name) ⇒ Object

def _parse_source( yaml, namespace )

`source #{yaml}`
ENV.select{|k,v| k.upcase =~ /^#{into.upcase}\_/}

end private :_parse_source



28
29
30
# File 'lib/pisec/settings.rb', line 28

def _open_file( file_name )
  File.open( file_name, "r" )
end

._parse_file(yaml) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pisec/settings.rb', line 33

def _parse_file( yaml )
  data_hash = {}
  yaml_io = _open_file( yaml )
  while !yaml_io.eof?
    l = yaml_io.readline
    next if l.match(/^\s*#/)
      m = l.chomp.match(/^\s*export\s+(.+)$/)
    next unless m && m[1]
    kv = m[1].split(/=/)
    #puts "got kv: #{kv.inspect}"
    hash_key = kv.first
    hash_value = JSON.parse(eval(kv.last))
    data_hash[hash_key] = hash_value
  end
  return data_hash
ensure
  yaml_io.close if yaml_io.respond_to?(:close)
end

.load(_data, into = nil) ⇒ Object



10
11
12
13
14
# File 'lib/pisec/settings.rb', line 10

def self.load _data, into=nil
  opts = {}
  opts[:namespace] = into if into
  new( _data, opts )
end

.load_file(yaml, into) ⇒ Object



5
6
7
8
# File 'lib/pisec/settings.rb', line 5

def self.load_file yaml, into
  _data = _parse_file( yaml )
  load( _data, into )
end

Instance Method Details

#==(other) ⇒ Object



60
61
62
63
# File 'lib/pisec/settings.rb', line 60

def ==(other)
  ( other.namespace == self.namespace ) &&
    ( other.data == self.data )
end

#get(key = '', _namespace = namespace) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/pisec/settings.rb', line 73

def get key='', _namespace = namespace
  result = nil
  if data && hash = data[_env_key_for(key, _namespace)]
    if hash.respond_to?(:[])
      result = hash[ key ]
    end
  end
  result || fail(RuntimeError)
end

#load(_data) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/pisec/settings.rb', line 65

def load _data
  if _data.respond_to?(:[])
    @data = _data
  else
    fail(RuntimeError)
  end
end