Module: Vfwcash

Defined in:
lib/vfwcash.rb,
lib/vfwcash/api.rb,
lib/vfwcash/cli.rb,
lib/models/gcash.rb,
lib/vfwcash/version.rb,
lib/vfwcash/controller.rb

Defined Under Namespace

Classes: Api, Checkbook, Controller, Gcash

Constant Summary collapse

LibPath =

Define constants to define where your are and location of config file

File.expand_path(File.dirname(__FILE__))
PWD =
Dir.pwd
VERSION =
"0.7.2"

Class Method Summary collapse

Class Method Details

.configObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/vfwcash.rb', line 11

def self.config
  if ENV['VFWCASHCONFIG'].present?
    config = YAML.load_file(ENV['VFWCASHCONFIG'])
  else
    config = YAML.load_file("#{PWD}/config/vfwcash.yml")
    
  end
  ENV['VFWCASHDATABASE'] = config[:database]
  return config
end

.install(options) ⇒ Object



81
82
83
84
85
86
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
118
# File 'lib/vfwcash.rb', line 81

def self.install(options)
  wd = PWD
  ok = true
  if options['dir'].present?
    unless Dir.exist?(wd+options['dir'])
      Dir.mkdir(wd+'/'+options['dir'])
      puts "Created a new directory in #{wd}"
      ok = false
      Dir.chdir(wd+'/'+options['dir'])
      wd = Dir.pwd
    end
  end
  unless Dir.exist?(wd+'/config')
    Dir.mkdir(wd+'/config')
    puts "Created config directory in #{wd}"
    ok = false
  end
  unless Dir.exist?(wd+'/pdf')
    Dir.mkdir(wd+'/pdf')
    puts "Created pdf directory in #{wd}"
    ok = false
  end
  unless File.exist?(wd+"/config/"+"vfwcash.yml")
    FileUtils.cp((LibPath+"/templates/vfwcash.yml"),(wd+"/config/"+"vfwcash.yml"))
    puts "Created vfwcash.yml file in config directory, must be edited for your post."
    ok = false
  end
   if options['db']
    unless File.exist?(wd+"/config/"+"vfwcash.gnucash")
      FileUtils.cp((LibPath+"/templates/vfwcash.gnucash"),(wd+"/config/"+"vfwcash.gnucash"))
      puts "Created vfwcash.gnucash db in config directory."
      ok = false
    end
  end

  puts "No installation required" if ok
  puts "Installation complete. Edit your vfwcash.yml file to set your post information." if !ok
end

.money(int) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/vfwcash.rb', line 66

def self.money(int)
  dollars = int / 100
  cents = (int % 100) / 100.0
  amt = dollars + cents
  set_zero = sprintf('%.2f',amt) # now have a string to 2 decimals
  set_zero.gsub(/(\d)(?=(\d{3})+(?!\d))/, "\\1,") # add commas
end

.set_date(date = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vfwcash.rb', line 34

def self.set_date(date=nil)
  if date.nil? || date == ""
    date = Date.today
  elsif date.class == Date
  else
    # has to be string
    if date.length == 6
      date += '01'
      date = Date.parse(date)
    else
      date = Date.parse(date)
    end
  end
  date
end

.str_date_range(from, to) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vfwcash.rb', line 50

def self.str_date_range(from,to)
  # used for 
  db = Tran.last..include?('-')
  from = Vfwcash.set_date(from)
  to = Vfwcash.set_date(to)
  from = from.to_datetime.beginning_of_day
  to = to.to_datetime.end_of_day
  
  if db
    return (from.to_s(:db))..(to.to_s(:db))
  else
    return (from.to_s(:number))..(to.to_s(:number))
  end
end

.transaction_rangeObject



27
28
29
30
31
32
# File 'lib/vfwcash.rb', line 27

def self.transaction_range
  t = Tran.order(:post_date)
  first = Date.parse(t.first.).beginning_of_month
  last = Date.parse(t.last.).end_of_month
  first..last
end

.valid_root?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
# File 'lib/vfwcash.rb', line 74

def self.valid_root?
  unless Dir.exist?(PWD+'/config') && Dir.exist?(PWD+'/pdf') && File.exist?(PWD+"/config/"+"vfwcash.yml")
    puts "Error: vfwcash must be run from a diectory containing valid  configuration files"
    exit(0)
  end
end

.yyyymm(date = nil) ⇒ Object



22
23
24
25
# File 'lib/vfwcash.rb', line 22

def self.yyyymm(date=nil)
  date = Vfwcash.set_date(date)
  yyyymm = date.strftime('%Y%m') # "#{date.year}#{date.month.to_s.rjust(2,'0')}"
end