number_to_love 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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YTI1YzU3ZmZjZWY2NjQxOWVlOWExNGJlNTc1MzYxOTZlMmE5MjQxOQ==
5
+ data.tar.gz: !binary |-
6
+ MjhhZDQ5ZTMyMmEzNTE2YThhN2M4NmY0MmI5Mzk1ODU2ZGY1ZTBmNA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MmY2MGIyZTFhYjMyZjc4NzVhMTY4NjJlYmI4NTU5Zjk0MDlkNzA0NzA5NzIy
10
+ MzE4MGMxYjQ1YWZkNzA3M2YyMTI0MjFjN2Q0M2I1MzYxMGE5NTBhYjM4YmIy
11
+ MThmNzUyODUzNGNhNGI1ZWFmOTIzOTlmZThkM2NmODFkNmE0Zjc=
12
+ data.tar.gz: !binary |-
13
+ YTYxMzE2ZDM0MTdjMzZhNzcwNzkwYzY5M2E1ZDA0Y2JiZWE4NzJmZjFjOWI0
14
+ NmViNTExNjQzNjViY2FiYjgxMzAyYTdkZWQ2NDkwZGFmNDU1NDQyYjYxNzI4
15
+ MmM0M2E5NTFjYzBjNTEzZDhmYWYyOTMyZGQ1M2JjYmY3OTYzOTM=
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.14.1"
10
+ gem "bundler"
11
+ gem "jeweler", "~> 1.8.7"
12
+ gem 'pry'
13
+ end
14
+
15
+
16
+ gem 'actionpack'
@@ -0,0 +1,101 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ actionpack (3.2.15)
5
+ activemodel (= 3.2.15)
6
+ activesupport (= 3.2.15)
7
+ builder (~> 3.0.0)
8
+ erubis (~> 2.7.0)
9
+ journey (~> 1.0.4)
10
+ rack (~> 1.4.5)
11
+ rack-cache (~> 1.2)
12
+ rack-test (~> 0.6.1)
13
+ sprockets (~> 2.2.1)
14
+ activemodel (3.2.15)
15
+ activesupport (= 3.2.15)
16
+ builder (~> 3.0.0)
17
+ activesupport (3.2.15)
18
+ i18n (~> 0.6, >= 0.6.4)
19
+ multi_json (~> 1.0)
20
+ addressable (2.3.5)
21
+ builder (3.0.4)
22
+ coderay (1.0.9)
23
+ diff-lcs (1.2.4)
24
+ erubis (2.7.0)
25
+ faraday (0.8.8)
26
+ multipart-post (~> 1.2.0)
27
+ git (1.2.6)
28
+ github_api (0.10.1)
29
+ addressable
30
+ faraday (~> 0.8.1)
31
+ hashie (>= 1.2)
32
+ multi_json (~> 1.4)
33
+ nokogiri (~> 1.5.2)
34
+ oauth2
35
+ hashie (2.0.5)
36
+ highline (1.6.20)
37
+ hike (1.2.3)
38
+ httpauth (0.2.0)
39
+ i18n (0.6.5)
40
+ jeweler (1.8.8)
41
+ builder
42
+ bundler (~> 1.0)
43
+ git (>= 1.2.5)
44
+ github_api (= 0.10.1)
45
+ highline (>= 1.6.15)
46
+ nokogiri (= 1.5.10)
47
+ rake
48
+ rdoc
49
+ journey (1.0.4)
50
+ json (1.8.1)
51
+ jwt (0.1.8)
52
+ multi_json (>= 1.5)
53
+ method_source (0.8.2)
54
+ multi_json (1.8.2)
55
+ multi_xml (0.5.5)
56
+ multipart-post (1.2.0)
57
+ nokogiri (1.5.10)
58
+ oauth2 (0.9.2)
59
+ faraday (~> 0.8)
60
+ httpauth (~> 0.2)
61
+ jwt (~> 0.1.4)
62
+ multi_json (~> 1.0)
63
+ multi_xml (~> 0.5)
64
+ rack (~> 1.2)
65
+ pry (0.9.12.2)
66
+ coderay (~> 1.0.5)
67
+ method_source (~> 0.8)
68
+ slop (~> 3.4)
69
+ rack (1.4.5)
70
+ rack-cache (1.2)
71
+ rack (>= 0.4)
72
+ rack-test (0.6.2)
73
+ rack (>= 1.0)
74
+ rake (10.1.0)
75
+ rdoc (3.12.2)
76
+ json (~> 1.4)
77
+ rspec (2.14.1)
78
+ rspec-core (~> 2.14.0)
79
+ rspec-expectations (~> 2.14.0)
80
+ rspec-mocks (~> 2.14.0)
81
+ rspec-core (2.14.7)
82
+ rspec-expectations (2.14.3)
83
+ diff-lcs (>= 1.1.3, < 2.0)
84
+ rspec-mocks (2.14.4)
85
+ slop (3.4.6)
86
+ sprockets (2.2.2)
87
+ hike (~> 1.2)
88
+ multi_json (~> 1.0)
89
+ rack (~> 1.0)
90
+ tilt (~> 1.1, != 1.3.0)
91
+ tilt (1.4.1)
92
+
93
+ PLATFORMS
94
+ ruby
95
+
96
+ DEPENDENCIES
97
+ actionpack
98
+ bundler
99
+ jeweler (~> 1.8.7)
100
+ pry
101
+ rspec (~> 2.14.1)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Dan Nguyen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "number_to_love"
18
+ gem.homepage = "http://github.com/dannguyen/number_to_love"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A wrapper around number_to_human}
21
+ gem.description = %Q{A wrapper around number_to_human}
22
+ gem.email = "dansonguyen@gmail.com"
23
+ gem.authors = ["Dan Nguyen"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "number_to_love #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,14 @@
1
+ require 'number_to_love/love_number'
2
+ module NumberToLove
3
+
4
+ def number_to_love(val, opts={})
5
+ LoveNumber.new(val, opts)
6
+ end
7
+
8
+ end
9
+
10
+
11
+ include NumberToLove
12
+
13
+
14
+
@@ -0,0 +1,121 @@
1
+ require 'action_view'
2
+ require 'delegate'
3
+
4
+ include ActionView::Helpers::NumberHelper
5
+
6
+ module NumberToLove
7
+ class LoveNumber < SimpleDelegator
8
+
9
+ HUMAN_ACCESSORS = [:precision, :significant, :units, :strip_insignificant_zeros, :delimiter, :separator, :format]
10
+
11
+ CUSTOM_ACCESSORS = [:minimalize]
12
+ ALL_ACCESSORS = HUMAN_ACCESSORS | CUSTOM_ACCESSORS
13
+
14
+ attr_accessor *ALL_ACCESSORS
15
+
16
+ VERBOSE_UNITS = {}
17
+ ABBREVIATED_DECIMAL_UNITS = {
18
+ unit: "",
19
+ # ten:
20
+ # one: Ten
21
+ # other: Tens
22
+ # hundred: Hundred
23
+ thousand: "K",
24
+ million: "M",
25
+ billion: "B",
26
+ trillion: "T"
27
+ }
28
+
29
+ def initialize(val, opts={})
30
+ @value = val
31
+ interpret_options(opts)
32
+
33
+ @units = ABBREVIATED_DECIMAL_UNITS
34
+
35
+
36
+ super(@value)
37
+ end
38
+
39
+ def to_v
40
+ @value
41
+ end
42
+
43
+
44
+ def to_s
45
+ opts = render_options
46
+ number_to_human(@value, opts[:number_to_human])
47
+ end
48
+
49
+
50
+ def inspect
51
+ to_s
52
+ end
53
+
54
+
55
+
56
+ # def method_missing(method_name, *args, &blk)
57
+ # if Numeric.method_defined?(method_name)
58
+ # @value.send(method_name, *args, &blk)
59
+ # elsif String.method_defined?(method_name)
60
+ # @self.to_s.send(method_name, *args, &blk)
61
+ # else
62
+ # super()
63
+ # end
64
+ # end
65
+
66
+
67
+ # def respond_to?(method_name, include_private = false)
68
+ # if String.method_defined?(method_name)
69
+ # true
70
+ # else
71
+ # super
72
+ # end
73
+ # end
74
+
75
+ (String.instance_methods - (Numeric.instance_methods | Float.instance_methods | Fixnum.instance_methods )).each do |foo|
76
+ define_method(foo) do |*args|
77
+ self.to_s.send(foo, *args)
78
+ end
79
+ end
80
+
81
+
82
+ private
83
+ # command
84
+ # sets attr_accessors
85
+ def interpret_options(opts)
86
+ opts.each_pair do |att, val|
87
+ a = att.to_sym
88
+ if ALL_ACCESSORS.include?(a)
89
+ self.instance_variable_set("@#{a}", val)
90
+ end
91
+
92
+
93
+ end
94
+ end
95
+
96
+ # returns a Hash to pass along
97
+ def render_options
98
+ hsh = {}
99
+ if minval = @minimalize
100
+ @units = ABBREVIATED_DECIMAL_UNITS
101
+ @format = "%n%u"
102
+ @precision = minval
103
+ @strip_insignificant_zeros = true
104
+ @delimiter = ''
105
+ @significant = true
106
+ end
107
+
108
+
109
+ hsh[:number_to_human] = HUMAN_ACCESSORS.inject({}) do |h, att|
110
+ if val = self.instance_variable_get("@#{att}")
111
+ h[att] = val
112
+ end
113
+
114
+ h
115
+ end
116
+
117
+ return hsh
118
+ end
119
+
120
+ end
121
+ end
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+
3
+ module NumberToLove
4
+ describe "LoveNumber" do
5
+
6
+ before(:each) do
7
+ @num = LoveNumber.new(423, precision: 2)
8
+ end
9
+
10
+ context 'instance methods' do
11
+ it '#to_v reflects original value' do
12
+ expect(@num.to_v).to eq 423
13
+ end
14
+
15
+ it '#to_s returns a string with options' do
16
+ expect(@num.to_s).to eq '420'
17
+ end
18
+ end
19
+
20
+ context "Acting like a number" do
21
+ context 'delegate to Numeric' do
22
+ it 'delegates to original number, i.e. :value' do
23
+ expect(@num.next).to eq 424
24
+ end
25
+ end
26
+
27
+ it 'should have equality based off of :value' do
28
+ expect(@num).to eq 423
29
+ end
30
+ end
31
+
32
+
33
+ context 'String.like behavior' do
34
+ it 'interpolates' do
35
+ expect("#{@num}").to eq "420"
36
+ end
37
+
38
+ it "delgates all non-numeric methods to String" do
39
+ # pending "hmmmm...."
40
+ bignum = LoveNumber.new(50000)
41
+ expect(bignum.upcase).to eq '50 K'
42
+ end
43
+ end
44
+
45
+
46
+ context 'default abbreviated units' do
47
+ before do
48
+ @num = LoveNumber.new(10000)
49
+ end
50
+
51
+ it 'should have abbreviated units by default' do
52
+ expect(@num.to_s).to eq "10 K"
53
+ end
54
+ end
55
+
56
+
57
+ context ':minimalize option' do
58
+
59
+ context 'default options' do
60
+ it 'abbreviated units with unspaced format' do
61
+ expect(number_to_love(65121, minimalize: 2).to_s).to eq "65K"
62
+ end
63
+
64
+ it 'should have no delimiters' do
65
+ expect(number_to_love(65121, minimalize: 3).to_s).to eq "65.1K"
66
+ end
67
+
68
+ it 'should override all other human options' do
69
+ t = number_to_love(65121, minimalize: true)
70
+ t.units = "%n %u"
71
+ t.precision = 10
72
+
73
+ expect(number_to_love(65121, minimalize: 2).to_s).to eq "65K"
74
+ end
75
+ end
76
+
77
+
78
+
79
+ end
80
+
81
+ context 'ad-hoc addition of options' do
82
+ it 'treats options as attr_accessors' do
83
+ @num.precision = 3
84
+ expect(@num.to_s).to eq "423"
85
+ end
86
+ end
87
+
88
+
89
+ end
90
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe "number_to_love" do
5
+
6
+ it 'is a helper method that creates a LoveNumber' do
7
+ expect(number_to_love(42)).to be_a LoveNumber
8
+ end
9
+
10
+
11
+ context 'options' do
12
+ describe 'number_to_human operators' do
13
+ it 'should respect :precision' do
14
+ expect(number_to_love(0.4201, precision: 2).to_s).to eq "0.42"
15
+ end
16
+
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'number_to_love'
5
+
6
+ require 'pry'
7
+ # Requires supporting files with custom matchers and macros, etc,
8
+ # in ./support/ and its subdirectories.
9
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
+
11
+ RSpec.configure do |config|
12
+
13
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: number_to_love
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dan Nguyen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.14.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 2.14.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: jeweler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.7
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.8.7
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A wrapper around number_to_human
84
+ email: dansonguyen@gmail.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ files:
90
+ - .document
91
+ - .rspec
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE.txt
95
+ - Rakefile
96
+ - VERSION
97
+ - lib/number_to_love.rb
98
+ - lib/number_to_love/love_number.rb
99
+ - spec/love_number_spec.rb
100
+ - spec/number_to_love_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: http://github.com/dannguyen/number_to_love
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.0.5
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: A wrapper around number_to_human
126
+ test_files: []