Class: Rigup::Utils::Config

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

Direct Known Subclasses

Config

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aDefaultValues, aNewValues = nil, &aBlock) ⇒ Config

Returns a new instance of Config.



7
8
9
10
11
12
13
# File 'lib/rigup/utils/config.rb', line 7

def initialize(aDefaultValues, aNewValues=nil, &aBlock)
  @default_values = aDefaultValues.clone
  reset()
  if aNewValues
    block_given? ? read(aNewValues, &aBlock) : read(aNewValues)
  end
end

Instance Attribute Details

#default_valuesObject (readonly)

Returns the value of attribute default_values.



5
6
7
# File 'lib/rigup/utils/config.rb', line 5

def default_values
  @default_values
end

Instance Method Details

#copy_booleans(aHash, *aKeys) ⇒ Object



121
122
123
124
125
# File 'lib/rigup/utils/config.rb', line 121

def copy_booleans(aHash, *aKeys)
  aKeys.each do |k|
    set_boolean(k, aHash[k])
  end
end

#copy_floats(aHash, *aKeys) ⇒ Object



115
116
117
118
119
# File 'lib/rigup/utils/config.rb', line 115

def copy_floats(aHash, *aKeys)
  aKeys.each do |k|
    set_float(k, aHash[k])
  end
end

#copy_ints(*aDb) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/rigup/utils/config.rb', line 107

def copy_ints(*aDb)
  aHash = aDb.shift
  aKeys = aDb
  aKeys.each do |k|
    set_int(k, aHash[k])
  end
end

#copy_item(aHash, aKey) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rigup/utils/config.rb', line 77

def copy_item(aHash, aKey)
  d = default_values[aKey]
  d_class = (d.is_a?(Class) ? d : d.class)
  cname = d_class.name.to_sym
  case cname
    when :NilClass then
      self[aKey] = aHash[aKey];
    when :String then
      self[aKey] = aHash[aKey].to_s unless aHash[aKey].nil?
    when :Float then
      set_float(aKey, aHash[aKey]);
    when :Fixnum then
      set_int(aKey, aHash[aKey]);
    when :TrueClass, :FalseClass then
      set_boolean(aKey, aHash[aKey]);
    when :Symbol then
      self[aKey] = (aHash[aKey].to_sym rescue nil)
    when :Proc then
      self[aKey] = aHash[aKey] if aHash[aKey].is_a?(Proc)
    else
      raise StandardError.new('unsupported type')
  end
end

#copy_strings(aHash, *aKeys) ⇒ Object



101
102
103
104
105
# File 'lib/rigup/utils/config.rb', line 101

def copy_strings(aHash, *aKeys)
  aKeys.each do |k|
    self[k] = aHash[k].to_s unless aHash[k].nil?
  end
end

#read(aSource, &aBlock) ⇒ Object

aBlock allows values to be filtered based on key,default and new values



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rigup/utils/config.rb', line 16

def read(aSource, &aBlock)
  default_values.each do |k, v|
    done = false
    if block_given? && ((newv = yield(k, v, aSource && aSource[k])) != nil)
      self[k] = newv
      done = true
    end
    copy_item(aSource, k) if !done && aSource && !aSource[k].nil?
  end
  self
end

#resetObject

reset values back to defaults



29
30
31
32
33
# File 'lib/rigup/utils/config.rb', line 29

def reset
  self.clear
  me = self
  @default_values.each { |n, v| me[n] = v.is_a?(Class) ? nil : v }
end

#set_boolean(aKey, aValue) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/rigup/utils/config.rb', line 57

def set_boolean(aKey, aValue)
  case aValue
    when TrueClass, FalseClass then
      self[aKey] = aValue;
    when String then
      self[aKey] = (['1', 'yes', 'y', 'true', 'on'].include?(aValue.downcase))
    else
      set_boolean(aKey, aValue.to_s)
  end
end

#set_float(aKey, aValue) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/rigup/utils/config.rb', line 46

def set_float(aKey, aValue)
  case aValue
    when String then
      self[aKey] = aValue.to_float(self[aKey]);
    when Fixnum then
      self[aKey] = aValue.to_f;
    when Float then
      self[aKey] = aValue;
  end
end

#set_int(aKey, aValue) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/rigup/utils/config.rb', line 35

def set_int(aKey, aValue)
  case aValue
    when String then
      self[aKey] = aValue.to_integer(self[aKey]);
    when Fixnum then
      self[aKey] = aValue;
    when Float then
      self[aKey] = aValue.to_i;
  end
end

#set_symbol(aKey, aValue) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/rigup/utils/config.rb', line 68

def set_symbol(aKey, aValue)
  case aValue
    when String then
      self[aKey] = (aValue.to_sym rescue nil);
    when Symbol then
      self[aKey] = aValue;
  end
end

#to_hashObject



127
128
129
# File 'lib/rigup/utils/config.rb', line 127

def to_hash
  {}.merge(self)
end