ken1flan / with_tax

対象の属性を指定すると税込計算用メソッドを追加するgem

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

WithTax

WithTaxはクラスにextendし、.attr_with_taxで属性を指定すると、#(指定した属性)_with_taxというメソッドで税込金額を求められるようになります。

Installation

Gemfileに下のように記述してください。

gem 'with_tax'

それから下を実行してください:

$ bundle install

または自分でインストールするには:

$ gem install with_tax

Usage

基本的な使い方

下のようにWithTaxextendし、attr_with_taxで対象の属性を指定してください。

class SampleItem
  extend WithTax

  attr_accessor :name, :price
  attr_with_tax :price

  def initialize(name, price)
    @name = name
    @price = price
  end
end

そうすると下のように属性名_with_taxというメソッドが利用できるようになり、税込み価格が取得できます。小数点以下は切り上げになっています。

sample_item = SampleItem.new('Some item', 123)
sample_item.price # => 123
sample_item.price_with_tax #=> 136

適用日

税率改定があるときに、計算の基準になる日を指定することもできます。

sample_item = SampleItem.new('Some item', 123)
sample_item.price # => 123
sample_item.price_with_tax(Date.parse('2019/09/30')) #=> 132

詳細設定

小数の取り扱い

デフォルトでは切り上げになっていますが、rounding_methodを設定することで切り替えることができます。

class SampleItem
  extend WithTax

  attr_accessor :name, :price
  attr_with_tax :price, rounding_method: :floor

  def initialize(name, price)
    @name = name
    @price = price
  end
end
切り捨て

:floorを設定すると小数点以下を切り捨てます。

class SampleItem
  extend WithTax

  attr_accessor :name, :price
  attr_with_tax :price, rounding_method: :floor

  def initialize(name, price)
    @name = name
    @price = price
  end
end

sample_item1 = SampleItem.new('Sample Item', 123)
sample_item1.price #=> 123
sample_item1.price_with_tax #=> 135

sample_item2 = SampleItem.new('Sample Item', 345)
sample_item2.price #=> 345
sample_item2.price_with_tax #=> 379
四捨五入

:roundを設定すると小数点以下を四捨五入します。

class SampleItem
  extend WithTax

  attr_accessor :name, :price
  attr_with_tax :price, rounding_method: :round

  def initialize(name, price)
    @name = name
    @price = price
  end
end

sample_item1 = SampleItem.new('Sample Item', 345)
sample_item1.price #=> 123
sample_item1.price_with_tax #=> 135

sample_item2 = SampleItem.new('Sample Item', 345)
sample_item2.price #=> 345
sample_item2.price_with_tax #=> 380
処理なし

nilを設定すると小数点以下を処理しません。

class SampleItem
  extend WithTax

  attr_accessor :name, :price
  attr_with_tax :price, rounding_method: nil

  def initialize(name, price)
    @name = name
    @price = price
  end
end

sample_item.price #=> 123
sample_item.price_with_tax #=> 135.3

軽減税率

デフォルトでは10%ですが、rate_type:reducedを設定することで軽減税率の8%に切り替えることができます。

class SampleItem
  extend WithTax

  attr_accessor :name, :price
  attr_with_tax :price, rate_type: :reduced

  def initialize(name, price)
    @name = name
    @price = price
  end
end
sample_item.price #=> 123
sample_item.price_with_tax #=> 133

複数のオプション設定

attr_with_maxは複数のオプションも設定できます。

class SampleItem
  extend WithTax

  attr_accessor :name, :price
  attr_with_tax :price, rounding_method: :round, rate_type: :reduced

  def initialize(name, price)
    @name = name
    @price = price
  end
end
sample_item.price #=> 123
sample_item.price_with_tax #=> 133

複数の属性への設定

attr_with_maxは複数の属性にも設定できます。

class SampleItem
  extend WithTax

  attr_accessor :name, :price, :price_takeout
  attr_with_tax :price, rounding_method: :round
  attr_with_tax :price_takeout, rounding_method: :round, rate_type: :reduced

  def initialize(name, price, price_takeout)
    @name = name
    @price = price
    @price_takeout = price_takeout
  end
end
sample_item.price #=> 123
sample_item.price_with_tax #=> 135
sample_item.price_takeout #=> 123
sample_item.price_takeout_with_tax #=> 133

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ken1flan/with_tax.

About

対象の属性を指定すると税込計算用メソッドを追加するgem


Languages

Language:Ruby 99.3%Language:Shell 0.7%