splittable 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 96ee5589ab0384576c8211075f5f05bb1bf6f5ede3ab118f928838cbd6d4ed4d
4
- data.tar.gz: 8e059c7013147f71b5327dec9ed17f0b0bcd44c787f25909df1a49750a985d25
3
+ metadata.gz: ad6faa4d272be234b860ea39ab7da30ec46b101ee1d53f3e452d332c13c13104
4
+ data.tar.gz: af7cd7ea65c72c019fbaf543e371ad6ecc4a184707ce5cd73e2779166f69d519
5
5
  SHA512:
6
- metadata.gz: 6bf5eae46d22553df1913201d78bcbab16be31c042281423db15d8d3830c4faf2284b561dd1729dd37db99ee8c6d8d4a0d4a33664e40ad5e9c977ab241d18f12
7
- data.tar.gz: 522f723448ec1f452c872064def09f61151504d6c0adb1e29af3008009dc49828d92cb9f195dddb3956214e811462682768c12bd259d103a8ad59966e9fdc1d0
6
+ metadata.gz: a9052a638a0e6295c13719d42356bbde2d1276da72d948fb728b9400fac254dc638680fda92f33ccc57b2f778318b9f1be8b3740b05af57a91005582281e906e
7
+ data.tar.gz: 3d6fce296eefb776df9994d8c68beaca1d436e5b30840b785a82ac5e2e6579f420376f9f28d2cd1dff68cd3ec517348ccc0f62a7334659994d82caf5afa39ee5
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## splittable 0.0.5 (Feb 02, 2021)
2
+
3
+ * Add precision parameter to division and normilize methods
4
+
5
+ *Ítalo Matos*
6
+
1
7
  ## splittable 0.0.4 (Jan 29, 2021)
2
8
 
3
9
  * Fix publish_release job
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- splittable (0.0.4)
4
+ splittable (0.0.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -36,6 +36,17 @@ Result: the total truncated value was divided by the number of plots informed an
36
36
  ```ruby
37
37
  => [0.5e-1, 0.3e-1, 0.3e-1] # => [0.05, 0.03, 0.03]
38
38
  ```
39
+ Default precision is 2 decimal places, but, you can customize this with precision parameter:
40
+
41
+ ``` ruby
42
+ Splittable.division(value: 10, quantity: 3, precision: 3)
43
+ ```
44
+
45
+ Result:
46
+ ```ruby
47
+ => [0.3334e1, 0.3333e1, 0.3333e1] # => [0.334, 0.333, 0.333]
48
+ ```
49
+
39
50
 
40
51
  Using `normalize` method:
41
52
 
@@ -49,6 +60,16 @@ Result: all values are truncated and them the difference is attributed in the fi
49
60
  => [0.3524e2, 0.2143e2, 0.4333e2] # => [35.24, 21.43, 43.33]
50
61
  ```
51
62
 
63
+ In this method, you have the same optional precision parameter:
64
+ ```ruby
65
+ Splittable.normalize(value: 100, installments: [33.33333333, 33.33333333, 33.33333333], precision: 3)
66
+ ```
67
+
68
+ Result:
69
+ ```ruby
70
+ => [0.33334e2, 0.33333e2, 0.33333e2] # => [33.334, 33.333, 33.333]
71
+ ```
72
+
52
73
  ## Development
53
74
 
54
75
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/lib/splittable.rb CHANGED
@@ -9,14 +9,14 @@ module Splittable
9
9
  class << self
10
10
  # receive total value and to quantity installments are required to equal division
11
11
  # just the first installment will receive the difference cents
12
- def division(value:, quantity:)
13
- Splittable::Division.new(value: value, quantity: quantity).call
12
+ def division(value:, quantity:, precision: 2)
13
+ Splittable::Division.new(value: value, quantity: quantity, precision: precision).call
14
14
  end
15
15
 
16
16
  # receive total value and specific value of installments are required to specific division
17
17
  # just the first installment will receive the difference cents
18
- def normalize(value:, installments:)
19
- Splittable::Normalize.new(value: value, installments: installments).call
18
+ def normalize(value:, installments:, precision: 2)
19
+ Splittable::Normalize.new(value: value, installments: installments, precision: precision).call
20
20
  end
21
21
  end
22
22
  end
@@ -1,15 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Splittable::Division
4
- def initialize(value:, quantity:)
5
- @value = BigDecimal(value, 15).truncate(2)
4
+ def initialize(value:, quantity:, precision: 2)
5
+ @precision = precision
6
+ @value = BigDecimal(value, 15).truncate(precision)
6
7
  @quantity = BigDecimal(quantity.to_i, 15)
7
8
 
8
9
  check_quantity_as_positive_value!
9
10
  end
10
11
 
11
12
  def call
12
- partial_value = (value / quantity).truncate(2)
13
+ partial_value = (value / quantity).truncate(precision)
13
14
  installments = [partial_value] * quantity
14
15
  installments[0] += value - installments.sum.to_d
15
16
 
@@ -18,7 +19,7 @@ class Splittable::Division
18
19
 
19
20
  private
20
21
 
21
- attr_reader :value, :quantity
22
+ attr_reader :value, :quantity, :precision
22
23
 
23
24
  def check_quantity_as_positive_value!
24
25
  return if quantity.positive?
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Splittable::Normalize
4
- def initialize(value:, installments:)
5
- @value = BigDecimal(value, 15).truncate(2)
6
- @installments = installments.map { |installment| BigDecimal(installment.round(2), 15) }
4
+ def initialize(value:, installments:, precision: 2)
5
+ @value = BigDecimal(value, 15).truncate(precision)
6
+ @installments = installments.map { |installment| BigDecimal(installment.round(precision), 15) }
7
7
  end
8
8
 
9
9
  def call
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Splittable
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.5'
5
5
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: splittable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arthur Brandão
8
8
  - Marcelo Toledo
9
- autorequire:
9
+ autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2021-02-01 00:00:00.000000000 Z
12
+ date: 2021-02-02 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: |-
15
15
  This gem solves the problem of several decimal places in divisions
@@ -56,7 +56,7 @@ metadata:
56
56
  homepage_uri: https://github.com/Pagnet/splittable
57
57
  source_code_uri: https://github.com/Pagnet/splittable
58
58
  changelog_uri: https://github.com/Pagnet/splittable/blob/master/CHANGELOG.md
59
- post_install_message:
59
+ post_install_message:
60
60
  rdoc_options: []
61
61
  require_paths:
62
62
  - lib
@@ -71,8 +71,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
71
  - !ruby/object:Gem::Version
72
72
  version: '0'
73
73
  requirements: []
74
- rubygems_version: 3.0.8
75
- signing_key:
74
+ rubygems_version: 3.2.3
75
+ signing_key:
76
76
  specification_version: 4
77
77
  summary: Calculate division and normalize parcels to use just cents.
78
78
  test_files: []