class TotalRecall::Config

Public Class Methods

new(options = {}) click to toggle source
# File lib/total_recall.rb, line 107
def initialize(options = {})
  options = { file: 'total_recall.yml' }.merge(options)
  @config_file = File.expand_path(options[:file])
  @transactions_only = !!options[:transactions_only]
end

Public Instance Methods

config() click to toggle source
# File lib/total_recall.rb, line 113
def config
  @config ||= YAML.load_file(@config_file)
end
context() click to toggle source
# File lib/total_recall.rb, line 160
def context
  @context ||= config[:context].merge(transactions: transactions)
end
csv() click to toggle source
# 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() click to toggle source
# File lib/total_recall.rb, line 117
def csv_file
  config[:csv][:file] &&
    File.expand_path(config[:csv][:file], File.dirname(@config_file))
end
ledger() click to toggle source
# File lib/total_recall.rb, line 206
def ledger
  tmp = @transactions_only ?
        transactions_only_template :
        template

  Mustache.render(tmp, context)
end
session() click to toggle source
# File lib/total_recall.rb, line 164
def session
  @session ||= session_class.new(transactions_config_defaults, :config => config)
end
session_class() click to toggle source
# 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 = {}, options = {})
        @config = options[:config]
        values.each do |k,v|
          self[k] = value_for(k, v)
        end
      end
    end
  end
end
template() click to toggle source
# File lib/total_recall.rb, line 134
def template
  @template ||= begin
    template_file ? File.read(template_file) : config[:template][:raw]
  end
end
template_file() click to toggle source
# File lib/total_recall.rb, line 129
def template_file
  config[:template][:file] &&
    File.expand_path(config[:template][:file], File.dirname(@config_file))
end
transaction_attributes() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/total_recall.rb, line 198
def transactions_config
  config[:context][:transactions]
end
transactions_config_defaults() click to toggle source
# File lib/total_recall.rb, line 202
def transactions_config_defaults
  transactions_config[:__defaults__] || {}
end
transactions_only_template() click to toggle source
# 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() click to toggle source
# 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