Class: MagicSheet::Sheet

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

Constant Summary collapse

DELEGATE =

Which methods to make available in the global scope

%w(rate fees convert_to chunk width log)
DEFAULT_WIDTH =
20

Instance Method Summary collapse

Constructor Details

#initializeSheet

Returns a new instance of Sheet.



59
60
61
62
63
# File 'lib/magicsheet.rb', line 59

def initialize
  @fees = []
  @chunk = 1
  @width = DEFAULT_WIDTH
end

Instance Method Details

#chunk(val) ⇒ Object



81
82
83
84
85
86
# File 'lib/magicsheet.rb', line 81

def chunk(val)
  unless val.between?(1,60)
    raise ArgumentError, "Chunk must be between 1 (minute-wise calculation) and 60 (full-hour calculation)"
  end
  @chunk = val
end

#convert_to(currency_name) ⇒ Object



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

def convert_to(currency_name)
  @convert_to = Currency[currency_name]
end

#fees(*list) ⇒ Object



77
78
79
# File 'lib/magicsheet.rb', line 77

def fees(*list)
  @fees = list.map {|f| Cost[f]}.compact
end

#log(str) ⇒ Object



102
103
104
# File 'lib/magicsheet.rb', line 102

def log(str)
  @log << ((str == '-') ? ''.rjust(@width,'-') : str.to_s.rjust(@width))
end

#processObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/magicsheet.rb', line 106

def process
  @log = []
  log "#{"%.2d" % @hours}:#{"%.2d" % @minutes}"
  
  if @chunk > 1 && @minutes > 0
    @minutes = (@minutes.to_f/@chunk).ceil * @chunk
    if @minutes == 60
      @hours += 1
      @minutes = 0
    end
  end
  
  log "= #{"%.2d" % @hours}:#{"%.2d" % @minutes}" if @chunk > 1
  
  #         if @minutes == 60
  #           @hours += 1
  #           @minutes = 0
  #         end
  #       end

  log "x #{@rate}"
  total = @hours * @rate.amount + (@minutes * @rate.amount/60.0)
  log '-'
  log "= #{Cost.new(total,@rate.currency)}"
  
  fees = false
  
  @fees.select {|f| f.currency == @rate.currency}.each do |f|
    fees = true
    total -= f.amount
    log "- #{f}"
  end
  
  if fees
    #log '-'
    log "= #{Cost.new(total,@rate.currency)}"
  end
  
  
  # Convert to target currency, if any
  if @convert_to && (@convert_to != @rate.currency)
    STDERR.puts "Getting conversion rate..."
    c = @rate.currency.conversion_rate_to(@convert_to)
    log ''
    log "x #{c}"
    log "(1h = #{Cost.new(@rate.amount * c,@convert_to)})"
    log ''
    total *= c
    log "= #{Cost.new(total,@convert_to)}"
    
    fees = false
    # Substract fees in target currency
    @fees.select {|f| f.currency == @convert_to}.each do |f|
      total -= f.amount
      log "- #{f}"
      fees = true
    end
    
    if fees
      log '-'
      log "= #{Cost.new(total,@convert_to)}"
    end
  end
  
  puts
  puts @log.join("\n")
end

#rate(rate) ⇒ Object



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

def rate(rate)
  @rate = Cost[rate]
end

#scanObject



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/magicsheet.rb', line 88

def scan
  STDERR.puts "Scanning sheet..."
  @hours, @minutes = 0, 0;
  if defined?(DATA)
    DATA.read.scan(/(\d+)\:(\d+)/) do |h,m|
      @hours += h.to_i
      @minutes += m.to_i
    end
    @hours += @minutes / 60
    @minutes = @minutes % 60
  end
  return self
end

#width(w) ⇒ Object



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

def width(w)
  @width = w
end