Guest User

Untitled

a guest
Jan 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. --- !ruby/object:ExecutablePage
  2. filename: ctfc4kkn385z8hwx57sk3t77qeh497b8ukb
  3. tags:
  4. :calc:
  5. :date: 2011-03-26 20:04:26.507557 -07:00
  6. :log:
  7. :programming:
  8. :ruby:
  9. :spreadsheet:
  10. data:
  11. :code: |-
  12. class ::Calculations
  13. class UnknownReference < NameError ; end
  14. def self.children ; @children ||= {} ; end
  15. def self.[] name
  16. # puts "Fetching #{name} from #{@names.length} names"
  17. obj = children[name]
  18. raise UnknownReference, "No such named-object as '#{name.inspect}'" unless obj
  19. obj
  20. end
  21. def self.new name, value
  22. children.member?(name) ? children[name].value = value : children[name] = super
  23. end
  24. def self.wipe
  25. @children = nil
  26. end
  27. attr_reader :dependents, :name, :requirements, :value
  28. def initialize name, value
  29. @name = name
  30. @value = value
  31. @dependents = []
  32. @requirements = []
  33. self
  34. end
  35. def invalidate
  36. # p [:inv, name, @dependents.length]
  37. @requirements = []
  38. @result = nil
  39. @dependents.each {|n| Show(n).invalidate }
  40. @dependents = [] # They're no-longer dependent on us until they re-reference us
  41. end
  42. def value= val
  43. invalidate unless val == value
  44. @value = val
  45. end
  46. def bind bnd, nm
  47. # p [:bind, nm, :for, name, :bnd, bnd]
  48. bnd.eval "#{nm} = Show(:#{nm}).calc"
  49. end
  50. def bind_all bnd
  51. requirements.each {|n| bind bnd, n }
  52. end
  53. def add_requirement nm
  54. # p [:ar, name, nm]
  55. raise "Re-adding requirement #{nm} to #{name}" if requirements.member? nm
  56. Show(nm).register_a_dependent name
  57. requirements << nm
  58. end
  59. def register_a_dependent who
  60. nm = who.respond_to?(:name) ? who.name : who
  61. dependents << nm unless dependents.member? nm
  62. end
  63. def run
  64. # p [:Cs,self.class.constants - Object.constants]
  65. bnd = binding
  66. # p [:bnd, bnd]
  67. bind_all bnd
  68. begin
  69. # puts "Trying lambda"
  70. v = bnd.eval formula, "solving for #{name}"
  71. v
  72. rescue NameError => e
  73. # puts "Rescued " + e
  74. if e.class == NameError
  75. raise "Not the right error '#{e.message}'" unless e.message =~ /^undefined local variable or method `\w+' for /
  76.  
  77. nm = e.message.scan(/`(\S+)'/).first.first.to_sym
  78. add_requirement nm
  79. bind bnd, nm
  80. retry
  81. else
  82. # puts "Rethrowing #{e.class}"
  83. raise
  84. end
  85. end
  86. end
  87. def formula
  88. return nil unless value.is_a?(String) && value[0..0] == '='
  89. value[( value[1..-1].index(/[^\s]/) + 1 )..-1]
  90. end
  91. def expand_formula recurse = 1, values = false
  92. # p [:ef1, name, :r, recurse, :v, values]
  93. # p [:v, value]
  94. calc rescue puts "Failed to fully graph #{name}" # resolve requirements as far as possible
  95. f = formula
  96. # p [:ef,:reql,requirements.length,:req,requirements]
  97. requirements.each {|n|
  98. r = Show(n)
  99. re = /#{n}/
  100. sf = if r.formula
  101. # p [:ef,:rf,r.formula]
  102. "(#{r.expand_formula(recurse - 1, values)})"
  103. else
  104. # p [:ef,:rv,r.value]
  105. "#{r.value.inspect}" if values
  106. end
  107. # p [:ef2, name, re, sf]
  108. f.gsub! re, sf if sf
  109. } unless recurse.zero?
  110. f
  111. end
  112. def calc
  113. @result ||= begin
  114. formula ? run : @value
  115. end
  116.  
  117. end
  118. end
  119. def Set *a ; Calculations.new *a ; end
  120. def Show n ; Calculations[n] ; end
  121. def Calc n
  122. begin
  123. Show(n).calc
  124. rescue
  125. raise unless $!.class <= NameError
  126. puts $!.message
  127. end
  128. end
  129. :custom_methods:
  130. :redit:
  131. :args: "*a"
  132. :body: |-
  133. # p [:edit_wrapper, a]
  134. results = self.class.instance_method(:edit).bind(self).call *a
  135. Thread.new {
  136. # sleep 5
  137. # print "Checking diff"
  138. unless diff.empty?
  139. # print " writing"
  140. write
  141. end
  142. # puts " done"
  143. }
  144. run
  145. results
  146. :run:
  147. :body: |-
  148. eval data[:code]
  149. data[:tax].each {|n,v| Set n, v }
  150. nil
  151. :examples: |-
  152. Calculations.wipe
  153. Set :wage, 10.25
  154. Set :hours, 2000
  155. Set :gross, '= wage * hours'
  156. Set :tax_rate, 0.35
  157. Set :tax, '= gross * tax_rate'
  158. Set :net, '= gross - tax'
  159. Calc :net
  160. Set :wage, 10.75 # a raise!
  161. Calc :net # automatically recalculates
  162. Show(:net).expand_formula(-1)
  163. :tax:
  164. - - :refund
  165. - "= refund_or_balance_owing < 0 ? refund_or_balance_owing.abs : 0"
  166. - - :refund_or_balance_owing
  167. - = total_tax_payable - total_credits
  168. - - :total_tax_payable
  169. - = net_federal_tax + bc_tax
  170. - - :net_federal_tax
  171. - = federal_tax
  172. - - :federal_tax
  173. - = basic_federal_tax
  174. - - :total_credits
  175. - = total_income_tax_deducted + bc_credits
  176. - - :bc_credits
  177. - 0
  178. - - :total_income_tax_deducted
  179. - 4249.82
  180. - - :basic_federal_tax
  181. - = s1_39 - s1_44
  182. - - :s1_39
  183. - = s1_37
  184. - - :s1_44
  185. - = total_federal_nonrefundable_tax_credits
  186. - - :s1_37
  187. - = taxable_income * federal_tax_rate
  188. - - :federal_tax_rate
  189. - = basic_federal_tax_rate
  190. - - :basic_federal_tax_rate
  191. - 0.15
  192. - - :total_federal_nonrefundable_tax_credits
  193. - = s1_25 * basic_federal_tax_rate
  194. - - :s1_25
  195. - = federal_basic_personal_amount + spouse_amount + cpp_contributions + ei_premiums + employment_amount
  196. - - :cpp_contributions
  197. - 1420.57
  198. - - :ei_premiums
  199. - 528.53
  200. - - :federal_basic_personal_amount
  201. - 10382
  202. - - :spouse_amount
  203. - 10382
  204. - - :employment_amount
  205. - = [1051, total_employment_income].min
  206. - - :total_employment_income
  207. - = employment_income
  208. - - :employment_income
  209. - = t4_1_14
  210. - - :t4_1_14
  211. - 31571.07
  212. - - :taxable_income
  213. - = net_income
  214. - - :net_income
  215. - = net_income_before_adjustments
  216. - - :net_income_before_adjustments
  217. - = total_income - annual_dues
  218. - - :annual_dues
  219. - 528.55
  220. - - :total_income
  221. - = employment_income + ei_and_other_benefits
  222. - - :ei_and_other_benefits
  223. - 3681.0
  224. - - :bc_tax
  225. - = taxable_income * bc_tax_rate - bc_nonrefundable_tax_credits
  226. - - :bc_tax_rate
  227. - 0.0506
  228. - - :bc_nonrefundable_tax_credits_subtotal
  229. - = bc_basic_personal_amount + bc_spouse_amount + cpp_contributions + ei_premiums
  230. - - :bc_basic_personal_amount
  231. - 11000
  232. - - :bc_spouse_amount
  233. - = [9653, bc_spouse_amount_subtotal].min
  234. - - :bc_spouse_amount_subtotal
  235. - = bc_spouse_basic_amount - spouse_net_income
  236. - - :bc_spouse_basic_amount
  237. - 10618
  238. - - :spouse_net_income
  239. - 0
  240. - - :bc_nonrefundable_tax_credits
  241. - = bc_nonrefundable_tax_credits_subtotal * bc_tax_rate
  242. - - :unreclaimed_tax
  243. - = total_income_tax_deducted - refund
Add Comment
Please, Sign In to add comment