Class: Flydata::Preference::DataEntryPreference

Inherits:
Object
  • Object
show all
Defined in:
lib/flydata/preference/data_entry_preference.rb

Constant Summary collapse

CONFS_HOME =
File.join(FLYDATA_HOME, 'confs')

Class Method Summary collapse

Class Method Details

.conf_exists?(data_entry) ⇒ Boolean

Returns:

  • (Boolean)

67
68
69
# File 'lib/flydata/preference/data_entry_preference.rb', line 67

def conf_exists?(data_entry)
  File.exists?(conf_path(data_entry))
end

.conf_name(data_entry) ⇒ Object


71
72
73
# File 'lib/flydata/preference/data_entry_preference.rb', line 71

def conf_name(data_entry)
  "#{data_entry['name']}.conf"
end

.conf_path(data_entry) ⇒ Object


63
64
65
# File 'lib/flydata/preference/data_entry_preference.rb', line 63

def conf_path(data_entry)
  "#{File.join(CONFS_HOME, data_entry['name'])}.conf"
end

.conf_template_name(data_entry) ⇒ Object


83
84
85
86
# File 'lib/flydata/preference/data_entry_preference.rb', line 83

def conf_template_name(data_entry)
  type = ActiveSupport::Inflector.underscore(data_entry['type'])
  "#{type}.conf.tmpl"
end

.conf_template_path(data_entry) ⇒ Object


79
80
81
# File 'lib/flydata/preference/data_entry_preference.rb', line 79

def conf_template_path(data_entry)
  File.join(FLYDATA_TMPL_DIR, conf_template_name(data_entry))
end

.configurable?(data_entry) ⇒ Boolean

Returns:

  • (Boolean)

75
76
77
# File 'lib/flydata/preference/data_entry_preference.rb', line 75

def configurable?(data_entry)
  File.exists?(conf_template_path(data_entry))
end

.copy_template(data_entry) ⇒ Object


52
53
54
55
56
57
58
59
60
61
# File 'lib/flydata/preference/data_entry_preference.rb', line 52

def copy_template(data_entry)
  return unless File.exists?(conf_template_path(data_entry))
  FileUtils.mkdir_p(CONFS_HOME)
  FileUtils.cp(conf_template_path(data_entry), conf_path(data_entry)+".tmpl")

  # Do not overwrite a current conf file
  unless File.exists?(conf_path(data_entry))
    FileUtils.cp(conf_template_path(data_entry), conf_path(data_entry))
  end
end

.filter_data_entry(de, source) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/flydata/preference/data_entry_preference.rb', line 29

def filter_data_entry(de, source)
  configurable_params = source.data_entry.config_params
  return de if configurable_params.nil? or configurable_params.empty?

  configurable_params.each do |pref_name, param_info|
    # skip if the data entry doesn't have the information
    de_pref = de[pref_name.to_s]
    next unless de_pref

    param_info.each do |param_name, options|
      next if options.nil? or options.empty?
      param_name_str = param_name.to_s
      next if de_pref[param_name_str].nil? or de_pref[param_name_str].empty?

      options.each do |option_name, option_value|
        de_pref[param_name_str] = Fluent::DataEntryPreferenceConfigurable.replace_value_with_option(
          param_name_str, de_pref[param_name_str], option_name, option_value, key: de['data_port_key'])
      end
    end
  end
end

.load_conf(data_entry, source) ⇒ Object

data_entry must be hash


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/flydata/preference/data_entry_preference.rb', line 11

def load_conf(data_entry, source)
  path = conf_path(data_entry)
  raise "Conf file does not exist. path:#{path}" unless File.exists?(path)
  custom_conf = YAML::load(File.open(path, 'r'))
  data_entry.each_key do |k|
    v = custom_conf[k]
    case v
    when Hash
      data_entry[k].merge!(v)
    when NilClass
    else
      data_entry[k] = v
    end
  end
  filter_data_entry(data_entry, source)
  data_entry
end