precision 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e9b2d511aad296086cbf8aae382977a01f3d2de6
4
+ data.tar.gz: 5f5e29f5be5e75211c2d98bc60956db7a6bc2c9e
5
+ SHA512:
6
+ metadata.gz: 8d63b2e2b96f8d30848f85aff7013e2c80807fb2e74446e918da6841a6f5911b421afc95443168a3077b9d1ee34c4afc7d21b484ec06f570f2dfacc064a8b386
7
+ data.tar.gz: 8fd6f754c902791b7ff900860434a0d44480aa70f961d22a6019862e01b80a23351e3720a7ef04e1572d5a4ca4f636b76180f4479764585e531cf18ab922df91
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in precision.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Sean Devine
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ ## Precision
2
+ Calculate the precision of anything that can be converted into a decimal.
3
+
4
+ ```ruby
5
+ irb> Precision::Calculator.new(BigDecimal("12.12")).calculate
6
+ => 2
7
+
8
+ irb> Precision::Calculator.new("12.1234").calculate
9
+ => 4
10
+
11
+ irb> Precision::Calculator.new(12.123).calculate
12
+ => 3
13
+
14
+ irb> Precision::Calculator.new(nil).calculate
15
+ => 0
16
+
17
+ # #calculate is aliased to #to_i
18
+ irb> precision = Precision::Calculator.new(1)
19
+ irb> precision.to_i == precision.calculate
20
+ => true
21
+ ```
22
+
23
+ ### Why might this be helpful?
24
+
25
+
26
+ Create the `precision` option argument using the current value of an attribute:
27
+
28
+ ```ruby
29
+ <%= number_to_currency @model.n, precision: Precision::Calculator.new(@model.n).to_i %>
30
+ ```
31
+
32
+ ## Installation
33
+
34
+
35
+ gem 'precision'
36
+
37
+ ## Contributions
38
+
39
+ - Created by [@barelyknown](https://twitter.com/barelyknown)
40
+ - You?
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ require "bigdecimal"
2
+
3
+ require "precision/version"
4
+ require "precision/calculator"
5
+
6
+ module Precision
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,31 @@
1
+ module Precision
2
+ class Calculator
3
+ attr_accessor :decimal
4
+
5
+ def initialize(decimal)
6
+ @decimal = BigDecimal(String(decimal))
7
+ end
8
+
9
+ def calculate
10
+ return 0 if significant_digits == "0"
11
+ [(significant_digits.length - exponent), 0].max
12
+ end
13
+
14
+ alias_method :to_i, :calculate
15
+
16
+ private
17
+
18
+ def significant_digits
19
+ split[1]
20
+ end
21
+
22
+ def exponent
23
+ split[3]
24
+ end
25
+
26
+ def split
27
+ @split ||= decimal.split
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Precision
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'precision/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "precision"
8
+ spec.version = Precision::VERSION
9
+ spec.authors = ["Sean Devine"]
10
+ spec.email = ["barelyknown@icloud.com"]
11
+ spec.summary = %q{Calculate the precision of a anything that can be converted into a decimal.}
12
+ spec.homepage = "https://github.com/barelyknown/precision"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.5"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec", ">= 3.0"
23
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ module Precision
4
+ describe Calculator do
5
+
6
+ scenarios = {
7
+ nil => 0,
8
+ "0.0" => 0,
9
+ "1.0" => 0,
10
+ "1.1" => 1,
11
+ "10.0" => 0,
12
+ "10.12" => 2,
13
+ 10.12 => 2,
14
+ BigDecimal("10.111") => 3,
15
+ 1 => 0
16
+ }
17
+
18
+ scenarios.each do |decimal, precision|
19
+ it "expects #{decimal.inspect} to have precision of #{precision}" do
20
+ calculator = described_class.new(decimal)
21
+ expect(calculator.calculate).to eq precision
22
+ end
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ require 'precision'
2
+
3
+ RSpec.configure do |config|
4
+
5
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: precision
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sean Devine
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - barelyknown@icloud.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/precision.rb
68
+ - lib/precision/calculator.rb
69
+ - lib/precision/version.rb
70
+ - precision.gemspec
71
+ - spec/lib/precision/calculator_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: https://github.com/barelyknown/precision
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.2.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Calculate the precision of a anything that can be converted into a decimal.
97
+ test_files:
98
+ - spec/lib/precision/calculator_spec.rb
99
+ - spec/spec_helper.rb
100
+ has_rdoc: