Class: TheFox::Wallet::AddCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/wallet/command_add.rb

Overview

Add a new Entry.

Constant Summary collapse

NAME =
'add'

Instance Method Summary collapse

Methods inherited from Command

create_by_name, #initialize, is_matching_class

Constructor Details

This class inherits a constructor from TheFox::Wallet::Command

Instance Method Details

#expense(expense_s) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/wallet/command_add.rb', line 107

def expense(expense_s)
  if !expense_s.nil? && expense_s.length > 0
    # Replace , with . in numbers. '1,23' means '1.23'.
    # eval the revenue so calculations are solved.
    # Expenses are always minus.
    -eval(expense_s.to_s.gsub(/,/, '.')).to_f.round(NUMBER_ROUND).abs
  end
end

#revenue(revenue_s) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/wallet/command_add.rb', line 98

def revenue(revenue_s)
  if !revenue_s.nil? && revenue_s.length > 0
    # Replace , with . in numbers. '1,23' means '1.23'.
    # eval the revenue so calculations are solved.
    eval(revenue_s.to_s.gsub(/,/, '.')).to_f.round(NUMBER_ROUND).abs
  end
end

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/wallet/command_add.rb', line 11

def run
  if @options[:entry_category].nil?
    @options[:entry_category] = 'default'
  end
  
  if @options[:is_interactively]
    # Interactive User Input
    
    print "title: [#{@options[:entry_title]}] "
    title_t = STDIN.gets.strip
    if title_t.length > 0
      # Search for '%d' in title.
      if @options[:entry_title] =~ /%d/
        # Like sprintf.
        @options[:entry_title] = @options[:entry_title] % title_t.split(',').map{ |s| s.strip }
      else
        @options[:entry_title] = title_t
      end
    end
    
    print "date: [#{@options[:entry_date]}] "
    date_t = STDIN.gets.strip
    if date_t.length > 0
      @options[:entry_date] = date_t
    end
    
    print "revenue: [#{@options[:entry_revenue]}] "
    revenue_t = revenue(STDIN.gets.strip)
    if !revenue_t.nil?
      @options[:entry_revenue] = revenue_t
    end
    
    print "expense: [#{@options[:entry_expense]}] "
    expense_t = expense(STDIN.gets.strip)
    if !expense_t.nil?
      @options[:entry_expense] = expense_t
    end
    
    print "category: [#{@options[:entry_category]}] "
    category_t = STDIN.gets.strip
    if category_t.length > 0
      @options[:entry_category] = category_t
    end
    
    print "comment: [#{@options[:entry_comment]}] "
    comment_t = STDIN.gets.strip
    if comment_t.length > 0
      @options[:entry_comment] = comment_t
    end
    
    puts '-' * 20
  end
  
  if @options[:entry_title].nil?
    raise "Option --title is required for command '#{NAME}'"
  end
  
  # --force option
  # --id    option
  is_unique = !@options[:force] && @options[:entry_id]
  
  puts "id:       '#{@options[:entry_id]}'"
  puts "title:    '#{@options[:entry_title]}'"
  puts "date:      " << Date.parse(@options[:entry_date]).to_s
  puts "revenue:   " << NUMBER_FORMAT % @options[:entry_revenue]
  puts "expense:   " << NUMBER_FORMAT % @options[:entry_expense]
  puts "balance:   " << NUMBER_FORMAT % [@options[:entry_revenue] + @options[:entry_expense]]
  puts "category:  #{@options[:entry_category]}"
  puts "comment:  '#{@options[:entry_comment]}'"
  puts "force:    #{@options[:force] ? 'YES' : 'NO'}"
  puts "unique:   #{is_unique ? 'YES' : 'NO'} (#{is_unique})"
  
  # Create new Entry.
  entry = Entry.new(@options[:entry_id], @options[:entry_title], @options[:entry_date], @options[:entry_revenue], @options[:entry_expense], @options[:entry_category], @options[:entry_comment])
  
  # Initialize Wallet.
  wallet = Wallet.new(@options[:wallet_path])
  
  # Add Entry to Wallet.
  added = wallet.add(entry, is_unique)
  
  puts "added:    #{added ? 'YES' : 'NO'}"
  
  added
end