Skip to content

Instantly share code, notes, and snippets.

@stephanschubert
Created August 21, 2009 10:18
Show Gist options
  • Save stephanschubert/171813 to your computer and use it in GitHub Desktop.
Save stephanschubert/171813 to your computer and use it in GitHub Desktop.
# [ Fixnum, Float ].each{ |klass| klass.send :include, Extensions::NetAndGross }
#
# Examples:
# 1.net # => 1
# 1.gross # => 1.19
# 1.gross.gross # => 1.19
# 1.gross.gross.net # => 1.0
#
module Extensions
module NetAndGross
def net
return self if net?
(self / net_to_gross_factor).net!
end
def gross
return self if gross?
(self * net_to_gross_factor).gross!
end
def gross?
!net?
end
def net?
@is_net = @is_net.nil? ? true : @is_net
end
def net!
@is_net = true
self
end
def gross!
@is_net = false
self
end
private
def net_to_gross_factor
1 + Configuration.sales_tax.to_f / 100
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment