hiro_format 0.0.2 → 0.0.3

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
  SHA1:
3
- metadata.gz: c1b0944ba990e29090b661200cdbe29963e1b388
4
- data.tar.gz: ad059da42abe249fd5b2c33d2b32ccdcf9884499
3
+ metadata.gz: 7ff2b0d324bdb47219ab870ce7e5f2cdb1e3f29d
4
+ data.tar.gz: c0a9629151e1091c587e4ac3f0fbb021d75431ef
5
5
  SHA512:
6
- metadata.gz: 2e51014e44811ffb27ec9df658328ee6efd2cf642e828e7953fce16540a31eef81518e16152efb38a52ada9f429e0cee288e8815762905051b73ab78d8520d6b
7
- data.tar.gz: 961c3417cd7ffa6338f4e9dc7fc8517e8035921af6ab57d43e3b1ae482b2dfcb002f4f5327dc831d46ef3ebb60334739dc3e3ec2626b4f3408b28b548387edad
6
+ metadata.gz: 648fade5b38f62a3a469dbe5b88cf4ead5c185d1137318b76c306ba516cd8f0ab3ce671d02f56943fafff148909ecb043597d76388c1d01cabc489c7beb38775
7
+ data.tar.gz: a06d3941839965b8baf697cc5b09493b6df42595a5c0de876800d1e49613b6113521221270fa78635fe098668b9bd45b2dbdadeb5b599d98df4bc1be3f76645d
data/README.md CHANGED
@@ -5,7 +5,11 @@ HiroFormat (hiro_format) is a text formatter.
5
5
  It converts ruby variables into formatted text by predefined recipe.
6
6
  It also supports ANSI color display for console applications, and html class handling to have colorful texts.
7
7
 
8
+ ## Still Buggy
8
9
 
10
+ Sorry, it is still in very early stage of development. If you use this, please update the gem frequently.
11
+
12
+ Bug reports are welcome.
9
13
 
10
14
  ## Installation
11
15
 
@@ -31,6 +35,7 @@ require 'hiro_format'
31
35
  puts "This is a text string".color(:red_marker).to_s
32
36
  value = 123456.78
33
37
  puts value.formatting(:commify).color(:blue).to_s # 123,456.78 (Blue color)
38
+ puts value.formatting(:euro_commify).color(:red).to_s # 123.456,78 (Red color)
34
39
  puts Formatting.new(Date.today, :jp_date).color(:magenta_marker).to_s # 2017-12-24
35
40
  puts Date.today.formatting(:euro_date).color(:reverse).to_s # 24-12-2017
36
41
  puts Formatting.new(Date.today, :us_date).color("magenta").to_span # <span class="magenta">12-24-2017</span>
@@ -13,15 +13,19 @@ APP_ROOT = File.dirname(__FILE__)
13
13
  $LOAD_PATH << "#{APP_ROOT}/lib"
14
14
  require "hiro_format/formatting"
15
15
  require "hiro_format/coloring"
16
- #require "pry" # gem install pry pry-byebug
16
+ # require "pry" # gem install pry pry-byebug
17
17
 
18
18
  value = 123456.78
19
+ print ":commify #{value.class}: "
19
20
  puts value.formatting(:commify).color(:blue).to_s
21
+ print ":euro_commify #{value.class}: "
22
+ puts value.formatting(:euro_commify).color(:red).to_s
20
23
  puts Formatting.new(Date.today, :us_date).color(:magenta_marker).to_s
21
24
  puts Date.today.formatting(:euro_date).color(:reverse).to_s
22
25
  puts Date.today.formatting(:machine_date).color(:cyan_bold).to_s
23
26
  puts "#pzm (Plus, Zero, Minus) can show in different color depending on the value passed."
24
27
  [-1, 0, 1].each do |n|
28
+ print "#{n.class}: "
25
29
  puts n.formatting(:digit6).pzm(:reverse, :red_marker, :magenta_marker).to_s
26
30
  end
27
31
  puts "Formatting helper for HTML, you set your color in CSS (Cyan Marker)".color(:cyan_marker).to_s
@@ -33,10 +33,12 @@ require 'date'
33
33
  :euro_datetimesecond => {:time_format => "%d-%m-%Y %H:%M:%s", :applicable => [Date, DateTime, Time], :rest => "0000-00-00 00:00:00" },
34
34
  :machine_datetime => {:time_format => "%Y%m%d%H%M", :applicable => [Date, DateTime], :rest => "000000000000" },
35
35
 
36
+ # Fixnum is deprecated in Ruby > 2.4.0 should be removed soon.
36
37
  :digit6 =>{:format => "%06d", :applicable => [Fixnum, Integer, Float], :rest => "000000"},
37
38
  :digit2 =>{:format => "%02d", :applicable => [Fixnum, Integer, Float], :rest => "00"},
38
39
 
39
- :func =>{:function => "", :applicable => [], :rest => ""},
40
+ :commify =>{:function => "commify", :applicable => [Integer, Float, String, NilClass], :rest => ""},
41
+ :euro_commify =>{:function => "euro_commify", :applicable => [Integer, Float, String, NilClass], :rest => ""},
40
42
  }.freeze
41
43
 
42
44
  def initialize(data, formatting_option=nil)
@@ -103,7 +105,7 @@ require 'date'
103
105
  elsif recipe[:format]
104
106
  result = recipe[:format] % @data
105
107
  elsif recipe[:function]
106
- Formatting.send(recipe[:function], @data)
108
+ result = Formatting.send(recipe[:function], @data)
107
109
  end
108
110
  else
109
111
  result = recipe[:rest]
@@ -143,12 +145,45 @@ require 'date'
143
145
  end
144
146
 
145
147
  def self.commify(num)
148
+ case num.class.to_s
149
+ when 'Float', 'Integer'
150
+ return Formatting.commify_string(num.to_s)
151
+ when 'String'
152
+ return Formatting.commify_string(num)
153
+ when 'NilClass'
154
+ return "0"
155
+ else
156
+ return "#{num.class}"
157
+ end
158
+ end
159
+
160
+ def self.commify_string(num)
146
161
  int, frac = *num.split(".")
147
162
  int = int.gsub(/(\d)(?=\d{3}+$)/, '\\1,')
148
163
  int << "." << frac if frac
149
164
  int
150
165
  end
151
166
 
167
+ def self.euro_commify(num)
168
+ case num.class.to_s
169
+ when 'Float', 'Integer'
170
+ return Formatting.euro_commify_string(num.to_s)
171
+ when 'String'
172
+ return Formatting.euro_commify_string(num)
173
+ when 'NilClass'
174
+ return "0"
175
+ else
176
+ return "#{num.class}"
177
+ end
178
+ end
179
+
180
+ def self.euro_commify_string(num)
181
+ int, frac = *num.split(".")
182
+ int = int.gsub(/(\d)(?=\d{3}+$)/, '\\1.')
183
+ int << "," << frac if frac
184
+ int
185
+ end
186
+
152
187
  end
153
188
 
154
189
  class NilClass
@@ -1,3 +1,3 @@
1
1
  module HiroFormat
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hiro_format
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiro Utsumi
@@ -53,7 +53,6 @@ files:
53
53
  - bin/console
54
54
  - bin/setup
55
55
  - formatting_samples.rb
56
- - hiro_format-0.0.1.gem
57
56
  - hiro_format.gemspec
58
57
  - lib/hiro_format.rb
59
58
  - lib/hiro_format/coloring.rb
Binary file