Class: FactReader

Inherits:
Object
  • Object
show all
Defined in:
lib/opensecret/plugins.io/facts/fact.reader.rb

Overview

– ——————————————————————— – # – Global (dictionary) fact manager. – # – ——————————————————————— – #

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fact_data, ini_filepath, group_symbol, key_symbol, key_value) ⇒ FactReader

- – Create a fact database given an INI file – as well as an initial 2D database entry, – and a parental fact database whose facts – can be reused. - – @f will be the referenciable object wide – name for the created fact database. - – Parameters – fact_data : parental fact database – ini_filepath : path to src INI factfile – group_symbol : for first param entry – key_symbol : for first param entry – key_value : for first param entry - – Dependencies and Assumptions – the [inifile] gem is installed – file exists at the ini_filepath – factory_facts are instantiated -



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/opensecret/plugins.io/facts/fact.reader.rb', line 42

def initialize fact_data, ini_filepath, group_symbol, key_symbol, key_value

  # -- ---------------------------------------------------- -- #
  # -- This time use param fact database for instantiation. -- #
  # -- ---------------------------------------------------- -- #
  @f = fact_data.nil? ? {} : fact_data

  # -- ------------------------------------------------ -- #
  # -- Add initializr (parameter) fact to the database. -- #
  # -- ------------------------------------------------ -- #
  add_fact group_symbol, key_symbol, key_value unless group_symbol.nil? && key_symbol.nil? && key_value.nil?

  # -- ------------------------------------------ -- #
  # -- Assimilate all the facts in this INI file. -- #
  # -- ------------------------------------------ -- #
  assimilate_ini_file ini_filepath

end

Instance Attribute Details

#fObject (readonly)

Returns the value of attribute f.



19
20
21
# File 'lib/opensecret/plugins.io/facts/fact.reader.rb', line 19

def f
  @f
end

Instance Method Details

#assimilate_property_file(property_filepath, group_symbol) ⇒ Object

– ——————————————- – # – Read the PROPERTY file key/value pairs – # – and store them in the fact database under – # – the group symbol specified. – # – – # – Can be used to assimilate files like the – # – ubiquitous application.properties into the – # – fact database. – # – – # – Subsequently, the incoming facts can then – # – be consumed, updated and deleted as per the – # – usual protocols. – # – ——————————————- – # – property file facts are NOT evaluated they – # – are simply assumed to be strings. – # – ——————————————- – # – Parameters – # – property_filepath : path to prop file – # – group_symbol : collection 4 facts – # – – # – Dependencies and Assumptions – # – properties file exists and formatted – # – group symbol not added if already exists – # – props are not eval’d - assumed 2b “strs” – # – ——————————————- – #



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/opensecret/plugins.io/facts/fact.reader.rb', line 87

def assimilate_property_file property_filepath, group_symbol

  return unless File.exists? property_filepath

  then_count = @f[group_symbol].nil? ? 0 : @f[group_symbol].length

  IO.foreach( property_filepath ) do |dirty_file_line|

    file_line = dirty_file_line.strip
    next if file_line.empty?
    next if file_line.start_with? "#"

    segments = file_line.split("=")
    raise_properrty_error if segments.length < 2
    property_value = file_line[(segments.first.length+1)..-1]

    key_symbol = segments.first.strip.gsub(".","_").to_sym
    add_fact group_symbol, key_symbol, property_value

  end

  now_count = @f[group_symbol].nil? ? 0 : @f[group_symbol].length

  log.info(ere) { "[fact reader] -------------------------------------------------------- -- #" }
  log.info(ere) { "[fact reader] THEN => [#{then_count}] properties existed (and)" }
  log.info(ere) { "[fact reader] NOW  => [#{now_count}] properties exist." }
  log.info(ere) { "[fact reader] -------------------------------------------------------- -- #" }
  LogObject.map @f[group_symbol]
  log.info(ere) { "[fact reader] -------------------------------------------------------- -- #" }

end

#raise_properrty_error(file_path, file_line, segments) ⇒ Object

– ——————————————————— – # – If a line in the property file contains less than two – # – segments (using equals = as the divider), raise error. – # – ——————————————————— – #



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/opensecret/plugins.io/facts/fact.reader.rb', line 124

def raise_properrty_error file_path, file_line, segments

    error_desc = "Expected app properties file line to contain at least one equals."
    error_vals = "Instead [#{segments.length}] parts were found in the array."
    error_line = "file path => #{file_path}"
    error_line = "file line => #{file_line}"
    error_pair = "key value => #{segments.to_s}"
    error_text = "\n\n#{error_desc}\n#{error_vals}\n\n#{error_line}\n\n#{error_pair}"
    raise SyntaxError.new error_text

end