numerology 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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +54 -0
- data/Rakefile +11 -0
- data/cucumber.yml +3 -0
- data/features/numerology.feature +19 -0
- data/features/step_definitions/numerology_steps.rb +11 -0
- data/features/support/env.rb +14 -0
- data/lib/numerology/version.rb +3 -0
- data/lib/numerology.rb +17 -0
- data/numerology.gemspec +20 -0
- metadata +93 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Steven Hammond
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Numerology
|
2
|
+
|
3
|
+
Format numbers with examples rather than complex codes. This can make your code more readable as well as open up opporunities for your users to specify their preferred output formats.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'numerology'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install numerology
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Adds the format_like method to Numeric. Pass a string example of the format you are looking and numerology will attempt to format the number similarly.
|
22
|
+
|
23
|
+
### Features
|
24
|
+
Currently the gem supports the following formatting options. Patches to add other formatting features are welcomed.
|
25
|
+
|
26
|
+
* Thousands separators.
|
27
|
+
* Significant digits
|
28
|
+
* Leading 0's
|
29
|
+
|
30
|
+
### Examples
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
num = 123456
|
34
|
+
num.format_like('1,111') >>> "123,456"
|
35
|
+
num.format_like('1.111') >>> "123.456"
|
36
|
+
num.format_like('1 111') >>> "123 456"
|
37
|
+
num.format_like('1,100') >>> "123,500"
|
38
|
+
|
39
|
+
1234.format_like('001,111') >>> "001,234"
|
40
|
+
12345.format_like('001,111') >>> "012,345"
|
41
|
+
```
|
42
|
+
If you are familiar with the syntax used by the stamp gem, which inspired this one, you can also use the following aliases for format_like:
|
43
|
+
|
44
|
+
* `stamp`
|
45
|
+
* `stamp_like`
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
1. Fork it
|
50
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
51
|
+
3. Implement your new features, with tests and update the documentation
|
52
|
+
4. Commit your changes (`git commit -am 'Added some feature'`)
|
53
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
6. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
require 'cucumber/rake/task'
|
4
|
+
|
5
|
+
Cucumber::Rake::Task.new(:features)
|
6
|
+
|
7
|
+
Cucumber::Rake::Task.new('features:wip', 'Run Cucumber features that are a work in progress') do |t|
|
8
|
+
t.profile = 'wip'
|
9
|
+
end
|
10
|
+
|
11
|
+
task :default => :features
|
data/cucumber.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
@numerology
|
2
|
+
Feature: Formatting a number
|
3
|
+
In order to format numbers
|
4
|
+
the format_like method
|
5
|
+
formats a number with a human readable example.
|
6
|
+
|
7
|
+
@integer
|
8
|
+
Scenario Outline: Format integers by example
|
9
|
+
Given the integer "<value>"
|
10
|
+
When I format using the example "<example>"
|
11
|
+
Then I produce "<output>"
|
12
|
+
|
13
|
+
Examples:
|
14
|
+
| value | example | output |
|
15
|
+
| 123456 | 1,111 | 123,456 |
|
16
|
+
| 123456 | 1.111 | 123.456 |
|
17
|
+
| 123456 | 1 111 | 123 456 |
|
18
|
+
| 1234567 | 1,111 | 1,234,567 |
|
19
|
+
| 1234567 | 1.111 | 1.234.567 |
|
@@ -0,0 +1,11 @@
|
|
1
|
+
Given /^the integer "(\d+)"$/ do |num|
|
2
|
+
@target = num.to_i
|
3
|
+
end
|
4
|
+
|
5
|
+
When /^I format using the example "(.*?)"$/ do |example|
|
6
|
+
@result = @target.format_like(example)
|
7
|
+
end
|
8
|
+
|
9
|
+
Then /^I produce "(.*?)"$/ do |expected|
|
10
|
+
assert_equal expected.strip, @result.strip
|
11
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
begin
|
3
|
+
Bundler.setup(:default, :development)
|
4
|
+
rescue Bundler::BundlerError => e
|
5
|
+
$stderr.puts e.message
|
6
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
7
|
+
exit e.status_code
|
8
|
+
end
|
9
|
+
|
10
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
|
11
|
+
require 'numerology'
|
12
|
+
|
13
|
+
require 'test/unit/assertions'
|
14
|
+
World(Test::Unit::Assertions)
|
data/lib/numerology.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "numerology/version"
|
2
|
+
|
3
|
+
module Numerology
|
4
|
+
def format_like(example)
|
5
|
+
return self.separate_thousands(example[-4]) if example =~ /(\D)\d{3}$/
|
6
|
+
end
|
7
|
+
|
8
|
+
def separate_thousands(separator)
|
9
|
+
self.to_s =~ /([^\.]*)(\..*)?/
|
10
|
+
int, dec = $1.reverse, $2 ? $2 : ""
|
11
|
+
while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2'+separator+'\3')
|
12
|
+
end
|
13
|
+
int.reverse + dec
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
Numeric.send(:include, ::Numerology)
|
data/numerology.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/numerology/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Steven Hammond"]
|
6
|
+
gem.email = ["shammond@patientslikeme.com"]
|
7
|
+
gem.description = %q{Format numbers using human readable examples.}
|
8
|
+
gem.summary = %q{Number formatting by example.}
|
9
|
+
gem.homepage = "https://github.com/patientslikeme/numerology"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "numerology"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Numerology::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rake'
|
19
|
+
gem.add_development_dependency 'cucumber'
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: numerology
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Steven Hammond
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: cucumber
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Format numbers using human readable examples.
|
47
|
+
email:
|
48
|
+
- shammond@patientslikeme.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- cucumber.yml
|
59
|
+
- features/numerology.feature
|
60
|
+
- features/step_definitions/numerology_steps.rb
|
61
|
+
- features/support/env.rb
|
62
|
+
- lib/numerology.rb
|
63
|
+
- lib/numerology/version.rb
|
64
|
+
- numerology.gemspec
|
65
|
+
homepage: https://github.com/patientslikeme/numerology
|
66
|
+
licenses: []
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.8.19
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: Number formatting by example.
|
89
|
+
test_files:
|
90
|
+
- features/numerology.feature
|
91
|
+
- features/step_definitions/numerology_steps.rb
|
92
|
+
- features/support/env.rb
|
93
|
+
has_rdoc:
|