Class: Flydata::Helper::Config
- Inherits:
-
Hash
- Object
- Hash
- Flydata::Helper::Config
- Defined in:
- lib/flydata/helper/config_parser.rb
Overview
This module parses a config file and return a key symbolized hash. Additionally config for the helper is replaced with Config instance
ex) helper.conf
# For helper(serverengine) workers: 2 # For helper helper:
scheduled_actions:
check_remote_actions:
check_interval: 10s
Constant Summary collapse
- DEFAULT_SCHEDULED_ACTIONS =
{ check_remote_actions: { check_interval: '30s', }, check_abnormal_shutdown: { check_interval: '60s', }, }
- DEFAULT_HELPER =
{ scheduled_actions: DEFAULT_SCHEDULED_ACTIONS, }
Class Method Summary collapse
- .config_param(name, format, option = {}) ⇒ Object
- .convert_format(format, value) ⇒ Object
- .create(hash) ⇒ Object
- .default ⇒ Object
- .time_value(str) ⇒ Object
Instance Method Summary collapse
- #fetch_scheduled_actions_conf(action_name, name, default = nil) ⇒ Object
-
#scheduled_actions ⇒ Object
Return deep copy.
Class Method Details
.config_param(name, format, option = {}) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/flydata/helper/config_parser.rb', line 95 def config_param(name, format, option = {}) method_name = name.to_s key = option[:key] || name default_value = option[:default] case option[:type] when :scheduled_actions define_method(method_name) do |action_name| Config.convert_format(format, fetch_scheduled_actions_conf( action_name, key, default_value)) end else define_method(method_name) do def_val = if default_value.respond_to?(:call) default_value.call(self) else default_value end Config.convert_format(format, self[key] || def_val) end end end |
.convert_format(format, value) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/flydata/helper/config_parser.rb', line 70 def convert_format(format, value) return nil if value.nil? case format when :time self.time_value(value) when :integer value.to_i when :float value.to_f when :string value.to_s when :bool case value when 'true' true when 'false' false else !!(value) end else raise "Invalid format:#{format}" end end |
.create(hash) ⇒ Object
34 35 36 |
# File 'lib/flydata/helper/config_parser.rb', line 34 def self.create(hash) self.new.merge(hash) end |
.default ⇒ Object
38 39 40 |
# File 'lib/flydata/helper/config_parser.rb', line 38 def self.default create(DEFAULT_HELPER) end |
.time_value(str) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/flydata/helper/config_parser.rb', line 55 def time_value(str) case str.to_s when /([0-9]+)s/ $~[1].to_i when /([0-9]+)m/ $~[1].to_i * 60 when /([0-9]+)h/ $~[1].to_i * 60*60 when /([0-9]+)d/ $~[1].to_i * 24*60*60 else str.to_f end end |
Instance Method Details
#fetch_scheduled_actions_conf(action_name, name, default = nil) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/flydata/helper/config_parser.rb', line 42 def fetch_scheduled_actions_conf(action_name, name, default = nil) value = if self[:scheduled_actions] and self[:scheduled_actions][action_name.to_sym] and self[:scheduled_actions][action_name.to_sym][name.to_sym] self[:scheduled_actions][action_name.to_sym][name.to_sym] else nil end value.nil? ? default : value end |
#scheduled_actions ⇒ Object
Return deep copy
141 142 143 144 145 146 147 148 149 |
# File 'lib/flydata/helper/config_parser.rb', line 141 def scheduled_actions actions = self[:scheduled_actions].dup self[:scheduled_actions].each do |k, v| actions[k] = v.dup actions[k][:name] = k actions[k][:check_interval] = Config.convert_format(:time, v) end actions end |