number_to_yen 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cd020005eff01385b01d43672f55c0fa9a8aabfd
4
+ data.tar.gz: e6c2df27f61b1eb0fd254f8b6876bb9e67c33a1c
5
+ SHA512:
6
+ metadata.gz: d652695add064fdf152cce7f959e0d4cd959e5e382e3f0940308e51f2e27b178632bd3417b6567ea530b78d443716dd50963d9cf84b03168d7e074f23cf50c1e
7
+ data.tar.gz: cc5b6863c21c6ab79b3fe7773acecccd5f622dfd3035c2703ef973ef59073f30e2e88171d142ef364dc0e215487935032398a1a626b18903f251414842b1db0b
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/bundle
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2
5
+ - 2.4
6
+ before_install: gem install bundler
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in number_to_yen.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Tsukuru Tanimichi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # number_to_yen
2
+
3
+ [![Build Status](https://travis-ci.org/ttanimichi/number_to_yen.svg?branch=master)](https://travis-ci.org/ttanimichi/number_to_yen)
4
+ [![Gem Version](https://badge.fury.io/rb/number_to_yen.svg)](http://badge.fury.io/rb/number_to_yen)
5
+
6
+ 数値を日本円の表記(例: 12万3,456円)の文字列にフォーマットする helper です。
7
+ Active Support の [number_to_currency](http://api.rubyonrails.org/v5.1.0/classes/ActiveSupport/NumberHelper.html#method-i-number_to_currency) に強く影響を受けています。
8
+
9
+ ```bash
10
+ $ gem install number_to_yen
11
+ ```
12
+
13
+ ```ruby
14
+ require 'number_to_yen'
15
+ include NumberToYen
16
+
17
+ number_to_yen(1234) # => "1,234円"
18
+ number_to_yen(123456) # => "12万3,456円"
19
+ number_to_yen(123456.0) # => "12万3,456円"
20
+ number_to_yen(1234567890) # => "12億3,456万7,890円"
21
+ ```
22
+
23
+ 4 桁区切りで 0 が 4 つ続く場合、日本語の慣習に倣ってそれらの桁は省略されます。
24
+
25
+ ```ruby
26
+ number_to_yen(120000) # => "12万円"
27
+ number_to_yen(1234560000) # => "12億3,456万円"
28
+ number_to_yen(1200007890) # => "12億7,890円"
29
+ number_to_yen(1200000000) # => "12億円"
30
+ ```
31
+
32
+ ## Ruby on Rails の場合
33
+
34
+ Gemfile に
35
+
36
+ ```ruby
37
+ gem 'number_to_yen'
38
+ ```
39
+
40
+ を追記して `bundle install` してください。include などは不要です。View の helper に `number_to_yen` が追加されます。
41
+
42
+ ```erb
43
+ <%= number_to_yen(123456) %> # => "12万3,456円"
44
+ ```
45
+
46
+ ## コア拡張を使う場合
47
+
48
+ Gemfile に次のように指定すると、Integer や Float にインスタンスメソッド `to_yen` が生えます。
49
+
50
+ ```ruby
51
+ gem 'number_to_yen', require: 'number_to_yen/core_ext'
52
+ ```
53
+
54
+ ```ruby
55
+ 1234.to_yen # => "1,234円"
56
+ 123456.to_yen # => "12万3,456円"
57
+ 123456.0.to_yen # => "12万3,456円"
58
+ 1234567890.to_yen # => "12億3,456万7,890円"
59
+ ```
60
+
61
+ より簡潔に書けて便利ですがグローバル汚染でもあるため、helper の `number_to_yen` とコア拡張の `to_yen` どちらを使うかは個別に判断してください。
62
+
63
+ ## `number_to_ja` と `Numeric#to_ja`
64
+
65
+ TODO: 記載する
66
+
67
+ ## gem 'draper' と併用する場合
68
+
69
+ TODO: 記載する
70
+
71
+ ## gem 'money' と併用する場合
72
+
73
+ TODO: 記載する
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "number_to_yen"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/number_to_yen ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "number_to_yen"
@@ -0,0 +1,52 @@
1
+ module NumberToYen
2
+ class Converter
3
+ def initialize(number, options = {})
4
+ unless number.respond_to?(:to_i)
5
+ fail ArgumentError, "You must pass something that responds to `to_i`: #{number.inspect}"
6
+ end
7
+
8
+ @number = number.to_i
9
+ @options = _symbolize_keys(options)
10
+ end
11
+
12
+ def convert
13
+ return '0' if @number.zero?
14
+
15
+ units.map.with_index { |unit, index|
16
+ unit_number =
17
+ if index.zero?
18
+ @number / unit[:value]
19
+ else
20
+ @number / unit[:value] % basic_unit
21
+ end
22
+
23
+ unless unit_number.zero?
24
+ s = unit_number.to_s
25
+ (1..s.length).select {|i| i % 4 == 0 }.each {|i| s.insert(-i, ',') }
26
+ "#{s}#{unit[:name]}"
27
+ end
28
+ }.join.sub(/\A[0,]+/, '')
29
+ end
30
+
31
+ private
32
+
33
+ def units
34
+ [
35
+ { value: basic_unit ** 4, name: '京' },
36
+ { value: basic_unit ** 3, name: '兆' },
37
+ { value: basic_unit ** 2, name: '億' },
38
+ { value: basic_unit ** 1, name: '万' },
39
+ { value: basic_unit ** 0, name: '' }
40
+ ]
41
+ end
42
+
43
+ def basic_unit
44
+ 10_000
45
+ end
46
+
47
+ # TODO: fake implementation
48
+ def _symbolize_keys(options)
49
+ options
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,5 @@
1
+ require 'number_to_yen/to_yen'
2
+
3
+ Numeric.class_eval do
4
+ include ::NumberToYen::ToYen
5
+ end
@@ -0,0 +1,9 @@
1
+ module NumberToYen
2
+ class Railtie < Rails::Railtie
3
+ initializer 'number_to_yen.configure_rails_initialization' do
4
+ ActiveSupport.on_load(:action_view) do
5
+ ActionView::Base.send(:include, ::NumberToYen)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ require 'number_to_yen/converter'
2
+ require 'number_to_yen/version'
3
+
4
+ module NumberToYen
5
+ module ToYen
6
+ def to_yen(options = {})
7
+ "#{to_ja(options)}円"
8
+ end
9
+
10
+ def to_ja(options = {})
11
+ ::NumberToYen::Converter.new(self, options).convert
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module NumberToYen
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,14 @@
1
+ require 'number_to_yen/converter'
2
+ require 'number_to_yen/version'
3
+
4
+ module NumberToYen
5
+ def number_to_yen(number, options = {})
6
+ "#{number_to_ja(number, options)}円"
7
+ end
8
+
9
+ def number_to_ja(number, options = {})
10
+ Converter.new(number, options).convert
11
+ end
12
+ end
13
+
14
+ require 'number_to_yen/railtie' if defined?(Rails)
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'number_to_yen/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'number_to_yen'
7
+ spec.version = NumberToYen::VERSION
8
+ spec.authors = ['Tsukuru Tanimichi']
9
+ spec.email = ['info+git@ttanimichi.com']
10
+
11
+ spec.summary = 'Formats a number into a Japanese Yen string (e.g., 12万3,456円).'
12
+ spec.homepage = 'https://github.com/ttanimichi/number_to_yen'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.required_ruby_version = '>= 2.2.0'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.14'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'minitest', '~> 5.0'
27
+ spec.add_development_dependency 'minitest-power_assert'
28
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: number_to_yen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tsukuru Tanimichi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-19 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-power_assert
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - info+git@ttanimichi.com
72
+ executables:
73
+ - number_to_yen
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - exe/number_to_yen
86
+ - lib/number_to_yen.rb
87
+ - lib/number_to_yen/converter.rb
88
+ - lib/number_to_yen/core_ext.rb
89
+ - lib/number_to_yen/railtie.rb
90
+ - lib/number_to_yen/to_yen.rb
91
+ - lib/number_to_yen/version.rb
92
+ - number_to_yen.gemspec
93
+ homepage: https://github.com/ttanimichi/number_to_yen
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: 2.2.0
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.6.11
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Formats a number into a Japanese Yen string (e.g., 12万3,456円).
117
+ test_files: []