Class: TotalRecall::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/total_recall.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config



64
65
66
67
# File 'lib/total_recall.rb', line 64

def initialize(options = {})
  options = {file: 'total_recall.yml'}.merge(options)
  @config_file = File.expand_path(options[:file])
end

Instance Method Details

#configObject



69
70
71
# File 'lib/total_recall.rb', line 69

def config
  @config ||= YAML.load_file(@config_file)
end

#contextObject



100
101
102
# File 'lib/total_recall.rb', line 100

def context
  @context ||= config[:context].merge(transactions: transactions)
end

#csvObject



78
79
80
81
82
83
# File 'lib/total_recall.rb', line 78

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_fileObject



73
74
75
76
# File 'lib/total_recall.rb', line 73

def csv_file
  config[:csv][:file] &&
    File.expand_path(config[:csv][:file], File.dirname(@config_file))
end

#ledgerObject



128
129
130
# File 'lib/total_recall.rb', line 128

def ledger
  Mustache.render(template, context)
end

#sessionObject



96
97
98
# File 'lib/total_recall.rb', line 96

def session
  @session ||= Helper.new(config)
end

#templateObject



90
91
92
93
94
# File 'lib/total_recall.rb', line 90

def template
  @template ||= begin
    template_file ? File.read(template_file) : config[:template][:raw]
  end
end

#template_fileObject



85
86
87
88
# File 'lib/total_recall.rb', line 85

def template_file
  config[:template][:file] &&
    File.expand_path(config[:template][:file], File.dirname(@config_file))
end

#transaction_defaultsObject



115
116
117
118
119
120
121
122
# File 'lib/total_recall.rb', line 115

def transaction_defaults
  @transaction_defaults ||= begin
    defaults = transactions_config[:__defaults__] || {}
    defaults.each_with_object({}) do |(k,v), result|
      result[k] = v.respond_to?(:call) ? session.with_row(nil, &v) : v
    end
  end
end

#transactionsObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/total_recall.rb', line 104

def transactions
  @transactions ||= begin
    csv.each_with_object([]) do |row, result|
      result << transaction_defaults.merge(transactions_config).each_with_object({}) do |(k,v), cfg|
        next if k[/^__/]
        cfg[k] = v.respond_to?(:call) ? session.with_row(row, &v) : v
      end
    end
  end
end

#transactions_configObject



124
125
126
# File 'lib/total_recall.rb', line 124

def transactions_config
  config[:context][:transactions]
end