Class: TotalRecall::Config
- Inherits:
-
Object
- Object
- TotalRecall::Config
- Defined in:
- lib/total_recall.rb
Instance Method Summary collapse
- #config ⇒ Object
- #context ⇒ Object
- #csv ⇒ Object
- #csv_file ⇒ Object
-
#initialize(options = {}) ⇒ Config
constructor
A new instance of Config.
- #ledger ⇒ Object
- #session ⇒ Object
- #session_class ⇒ Object
- #template ⇒ Object
- #template_file ⇒ Object
- #transaction_attributes ⇒ Object
- #transactions ⇒ Object
- #transactions_config ⇒ Object
- #transactions_config_defaults ⇒ Object
- #transactions_only_template ⇒ Object
- #transactions_tokens ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Config
Returns a new instance of Config.
107 108 109 110 111 |
# File 'lib/total_recall.rb', line 107 def initialize( = {}) = { file: 'total_recall.yml' }.merge() @config_file = File.([:file]) @transactions_only = !![:transactions_only] end |
Instance Method Details
#config ⇒ Object
113 114 115 |
# File 'lib/total_recall.rb', line 113 def config @config ||= YAML.load_file(@config_file) end |
#context ⇒ Object
160 161 162 |
# File 'lib/total_recall.rb', line 160 def context @context ||= config[:context].merge(transactions: transactions) end |
#csv ⇒ Object
122 123 124 125 126 127 |
# File 'lib/total_recall.rb', line 122 def csv @csv ||= begin csv_raw = csv_file ? File.read(csv_file) : config[:csv][:raw] CSV.parse(csv_raw, config[:csv][:options] || {}) end end |
#csv_file ⇒ Object
117 118 119 120 |
# File 'lib/total_recall.rb', line 117 def csv_file config[:csv][:file] && File.(config[:csv][:file], File.dirname(@config_file)) end |
#ledger ⇒ Object
206 207 208 209 210 211 212 |
# File 'lib/total_recall.rb', line 206 def ledger tmp = @transactions_only ? transactions_only_template : template Mustache.render(tmp, context) end |
#session ⇒ Object
164 165 166 |
# File 'lib/total_recall.rb', line 164 def session @session ||= session_class.new(transactions_config_defaults, :config => config) end |
#session_class ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/total_recall.rb', line 175 def session_class @session_class ||= begin Class.new(Struct.new(*transaction_attributes)) do include SessionHelper def initialize(values = {}, = {}) @config = [:config] values.each do |k,v| self[k] = value_for(k, v) end end end end end |
#template ⇒ Object
134 135 136 137 138 |
# File 'lib/total_recall.rb', line 134 def template @template ||= begin template_file ? File.read(template_file) : config[:template][:raw] end end |
#template_file ⇒ Object
129 130 131 132 |
# File 'lib/total_recall.rb', line 129 def template_file config[:template][:file] && File.(config[:template][:file], File.dirname(@config_file)) end |
#transaction_attributes ⇒ Object
168 169 170 171 172 173 |
# File 'lib/total_recall.rb', line 168 def transaction_attributes @transaction_attributes ||= begin transactions_config.dup.delete_if{|k,_| k[/__/]}.keys | transactions_config_defaults.keys end end |
#transactions ⇒ Object
190 191 192 193 194 195 196 |
# File 'lib/total_recall.rb', line 190 def transactions @transactions ||= begin csv.each_with_object([]) do |row, transactions| transactions << Hash[session.extract_transaction(row).each_pair.to_a] end end end |
#transactions_config ⇒ Object
198 199 200 |
# File 'lib/total_recall.rb', line 198 def transactions_config config[:context][:transactions] end |
#transactions_config_defaults ⇒ Object
202 203 204 |
# File 'lib/total_recall.rb', line 202 def transactions_config_defaults transactions_config[:__defaults__] || {} end |
#transactions_only_template ⇒ Object
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/total_recall.rb', line 140 def transactions_only_template @transactions_only_template ||= begin Mustache::Template.new("").tap do |t| _transactions_tokens = proc { transactions_tokens } t.define_singleton_method(:tokens) do |*| _transactions_tokens.call end end end end |
#transactions_tokens ⇒ Object
151 152 153 154 155 156 157 158 |
# File 'lib/total_recall.rb', line 151 def transactions_tokens @transactions_tokens ||= begin Mustache::Template.new(template).tokens.detect do |type, tag, *rest| type == :mustache && tag == :section && [:mustache, :fetch, ["transactions"]] end end end |