hiro_format 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7ff2b0d324bdb47219ab870ce7e5f2cdb1e3f29d
4
- data.tar.gz: c0a9629151e1091c587e4ac3f0fbb021d75431ef
3
+ metadata.gz: fbd05ad43abfdead030270847cfbb1ee175059d3
4
+ data.tar.gz: b131ba35f8d03322e97bab7d2f8a49e92ae55356
5
5
  SHA512:
6
- metadata.gz: 648fade5b38f62a3a469dbe5b88cf4ead5c185d1137318b76c306ba516cd8f0ab3ce671d02f56943fafff148909ecb043597d76388c1d01cabc489c7beb38775
7
- data.tar.gz: a06d3941839965b8baf697cc5b09493b6df42595a5c0de876800d1e49613b6113521221270fa78635fe098668b9bd45b2dbdadeb5b599d98df4bc1be3f76645d
6
+ metadata.gz: 9f55048df4a8dcaa3c73f7ba03740ce68e55f672ab0295831759bec7591bd390d0fa93ce84e6c20feb5bb3761b371697ac32aa8eacf8e7ad412914f15ce77ec9
7
+ data.tar.gz: ad68d0cf118f665c65e1f4bd4921d84e8ab91bbfd7fb1ed004637eb441331bc96a0ba1a506217d71d3ffd9668668fc1c56b6da9a9088e5ac174ff953cffd451f
data/Formatting.md ADDED
@@ -0,0 +1,34 @@
1
+ # Supported Recipes
2
+
3
+ FORMATTER_LIST = {
4
+
5
+ :date => {:time_format => "%Y-%m-%d", :applicable => [Date, DateTime], :rest => "0000-00-00" },
6
+ :jp_date => {:time_format => "%Y-%m-%d", :applicable => [Date, DateTime], :rest => "0000-00-00" },
7
+ :us_date => {:time_format => "%m-%d-%Y", :applicable => [Date, DateTime], :rest => "0000-00-00" },
8
+ :euro_date => {:time_format => "%d-%m-%Y", :applicable => [Date, DateTime], :rest => "0000-00-00" },
9
+ :obvious_date => {:time_format => "%d-%b-%Y", :applicable => [Date, DateTime], :rest => "0000-00-00" },
10
+ :machine_date => {:time_format => "%Y%m%d", :applicable => [Date, DateTime], :rest => "00000000" },
11
+
12
+ :datewday => {:time_format => "%Y-%m-%d (%a)", :applicable => [Date, DateTime], :rest => "0000-00-00 (---)" },
13
+ :jp_datewday => {:time_format => "%Y-%m-%d (%a)", :applicable => [Date, DateTime], :rest => "0000-00-00 (---)" },
14
+ :us_datewday => {:time_format => "%m-%d-%Y (%a)", :applicable => [Date, DateTime], :rest => "00-00-0000 (---)" },
15
+ :euro_datewday => {:time_format => "%d-%m-%Y (%a)", :applicable => [Date, DateTime], :rest => "00-00-0000 (---)" },
16
+
17
+ :datetime => {:time_format => "%Y-%m-%d %H:%M", :applicable => [Date, DateTime, Time], :rest => "0000-00-00" },
18
+ :datetimesecond => {:time_format => "%Y-%m-%d %H:%M%s", :applicable => [Date, DateTime, Time], :rest => "0000-00-00" },
19
+ :jp_datetime => {:time_format => "%Y-%m-%d %H:%M", :applicable => [Date, DateTime, Time], :rest => "0000-00-00" },
20
+ :jp_datetimesecond => {:time_format => "%Y-%m-%d %H:%M:%s", :applicable => [Date, DateTime, Time], :rest => "0000-00-00 00:00:00" },
21
+ :us_datetime => {:time_format => "%m-%d-%Y %H:%M", :applicable => [Date, DateTime, Time], :rest => "00-00-0000 00:00" },
22
+ :us_datetimesecond => {:time_format => "%m-%d-%Y %H:%M:%s", :applicable => [Date, DateTime, Time], :rest => "00-00-0000 00:00:00" },
23
+ :euro_datetime => {:time_format => "%d-%m-%Y %H:%M", :applicable => [Date, DateTime, Time], :rest => "00-00-0000 00:00" },
24
+ :euro_datetimesecond => {:time_format => "%d-%m-%Y %H:%M:%s", :applicable => [Date, DateTime, Time], :rest => "0000-00-00 00:00:00" },
25
+ :machine_datetime => {:time_format => "%Y%m%d%H%M", :applicable => [Date, DateTime], :rest => "000000000000" },
26
+
27
+ # Fixnum is deprecated in Ruby > 2.4.0 should be removed soon.
28
+ :digit6 =>{:format => "%06d", :applicable => [Fixnum, Integer, Float], :rest => "000000"},
29
+ :digit2 =>{:format => "%02d", :applicable => [Fixnum, Integer, Float], :rest => "00"},
30
+
31
+ :commify =>{:function => "commify", :applicable => [Integer, Float, String, NilClass], :rest => ""},
32
+ :euro_commify =>{:function => "euro_commify", :applicable => [Integer, Float, String, NilClass], :rest => ""},
33
+
34
+ }.freeze
data/README.md CHANGED
@@ -5,6 +5,13 @@ 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
+ This formatter uses ruby symbols to specify formats.
9
+
10
+ It is nice to have a def like "MyString".red , but by using symbols it gives you much more flexibilities to handle data of database objects, arrays, hashes.
11
+
12
+ See 'Usage hint for Active Record / Sequel users'
13
+
14
+
8
15
  ## Still Buggy
9
16
 
10
17
  Sorry, it is still in very early stage of development. If you use this, please update the gem frequently.
@@ -27,7 +34,7 @@ Or install it yourself as:
27
34
 
28
35
  $ gem install hiro_format
29
36
 
30
- ## Usage samples
37
+ ## Usage
31
38
 
32
39
  ~~~ruby
33
40
  require 'hiro_format'
@@ -36,26 +43,71 @@ puts "This is a text string".color(:red_marker).to_s
36
43
  value = 123456.78
37
44
  puts value.formatting(:commify).color(:blue).to_s # 123,456.78 (Blue color)
38
45
  puts value.formatting(:euro_commify).color(:red).to_s # 123.456,78 (Red color)
46
+ puts value.formatting(:euro).to_s # € 123.456,78
47
+
39
48
  puts Formatting.new(Date.today, :jp_date).color(:magenta_marker).to_s # 2017-12-24
40
49
  puts Date.today.formatting(:euro_date).color(:reverse).to_s # 24-12-2017
41
50
  puts Formatting.new(Date.today, :us_date).color("magenta").to_span # <span class="magenta">12-24-2017</span>
42
51
  # pzm means Plus/Zero/Minus
43
- puts -1.formatting(:digit2).pzm?(:green, :hide, :red).to_s # -01 (Red)
44
- puts 0.formatting(:digit2).pzm?(:green, :hide, :red).to_s # (No show)
45
- puts 1.formatting(:digit6).pzm(value, :green, :hide, :red).to_s # 000002 (Green)
52
+ puts -1.formatting(:digit6).pzm?(12, [:green, :hide, :red]).to_s # -01 (Green)
53
+ puts 0.formatting(:digit6).pzm?(0, [:green, :hide, :red]).to_s # (No show)
54
+ puts 1.formatting(:digit6).pzm([:green, :hide, :red]).to_s # 000002 (Green)
55
+ ~~~
56
+
57
+ See formatting_sample.rb for more examples.
58
+
59
+
60
+ ## Usage hint for Active Record / Sequel users
61
+
62
+ ~~~ruby
63
+ # require "hiro_format" # you need gem install hiro_format
64
+ require "./lib/hiro_format/formatting"
65
+ require "./lib/hiro_format/coloring"
66
+
67
+ FIELD_LIST = [
68
+ {:title => "ID", :key => :id, :format => nil},
69
+ {:title => "Name", :key => :name, :format => :none, :color => "blue_class"},
70
+ {:title => "BirthDay", :key => :bday, :format => :obvious_date, },
71
+ {:title => "Assets", :key => :assets, :format => :euro, :pzm => ["green_class", "blue_class", "red_class"]},
72
+ ]
73
+ # @records = MyFriend.all
74
+ # For test without database engine
75
+ @records = [
76
+ {:id => 1, :name => "John", :bday => Date.parse('2000-01-02'), :assets => '1000000'},
77
+ {:id => 2, :name => "Kevin", :bday => Date.parse('2000-04-12'), :assets => '-10000'},
78
+ ]
79
+ puts "<table>"
80
+ puts "<tr>"
81
+ FIELD_LIST.each do |field|
82
+ puts "<th>#{field[:title]}</th>"
83
+ end
84
+ puts "</tr>"
85
+ @records.each do |my_friend|
86
+ puts "<tr>"
87
+ FIELD_LIST.each do |field|
88
+ if field[:pzm]
89
+ puts my_friend[field[:key]].formatting(field[:format]).pzm(field[:pzm]).to_td
90
+ else
91
+ puts my_friend[field[:key]].formatting(field[:format]).color(field[:color]).to_td
92
+ end
93
+ end
94
+ puts "</tr>"
95
+ end
96
+ puts "</table>"
46
97
  ~~~
47
98
 
48
99
  ## Formatting Recipes
49
100
 
50
101
  :date, :datetime, :machine_date, :commify, and more
51
102
 
52
- See Help file for details at : (TODO)
103
+ See Help file for details : Formatting.md
53
104
 
54
105
  ## Coloring Recipes
55
106
 
56
107
  :hide, :red, :red_bold, :red_back, :red_marker, :yellow, :reverse, and more
57
108
 
58
- See Help file for details at : (TODO)
109
+ Run formatting_sample.rb to see a list of supported recipes.
110
+
59
111
 
60
112
  ## TODO
61
113
 
@@ -34,11 +34,20 @@ require 'date'
34
34
  :machine_datetime => {:time_format => "%Y%m%d%H%M", :applicable => [Date, DateTime], :rest => "000000000000" },
35
35
 
36
36
  # Fixnum is deprecated in Ruby > 2.4.0 should be removed soon.
37
- :digit6 =>{:format => "%06d", :applicable => [Fixnum, Integer, Float], :rest => "000000"},
38
- :digit2 =>{:format => "%02d", :applicable => [Fixnum, Integer, Float], :rest => "00"},
37
+ :digit6 => {:format => "%06d", :applicable => [Fixnum, Integer, Float], :rest => "000000"},
38
+ :digit2 => {:format => "%02d", :applicable => [Fixnum, Integer, Float], :rest => "00"},
39
39
 
40
- :commify =>{:function => "commify", :applicable => [Integer, Float, String, NilClass], :rest => ""},
41
- :euro_commify =>{:function => "euro_commify", :applicable => [Integer, Float, String, NilClass], :rest => ""},
40
+ :commify => {:function => "commify", :applicable => [Integer, Float, String, NilClass], :rest => ""},
41
+ :euro_commify => {:function => "euro_commify", :applicable => [Integer, Float, String, NilClass], :rest => ""},
42
+ :commify0 => {:function => "commify0", :applicable => [Integer, Float, String, NilClass], :rest => ""},
43
+ :euro_commify0 => {:function => "euro_commify0", :applicable => [Integer, Float, String, NilClass], :rest => ""},
44
+ :commify4 => {:function => "commify4", :applicable => [Integer, Float, String, NilClass], :rest => ""},
45
+ :euro_commify4 => {:function => "euro_commify4", :applicable => [Integer, Float, String, NilClass], :rest => ""},
46
+
47
+ :japanese_yen => {:function => "japanese_yen", :applicable => [Integer, Float, String, NilClass], :rest => ""},
48
+ :euro => {:function => "euro", :applicable => [Integer, Float, String, NilClass], :rest => ""},
49
+
50
+ :hide => {:format => "", :applicable => [], :rest => ""},
42
51
  }.freeze
43
52
 
44
53
  def initialize(data, formatting_option=nil)
@@ -51,26 +60,19 @@ require 'date'
51
60
  self
52
61
  end
53
62
 
54
- def pzm(plus, zero, minus)
55
- judge = @data.to_f
56
- if judge > 0.0
57
- @color_scheme = plus
58
- elsif judge < 0.0
59
- @color_scheme = minus
60
- else
61
- @color_scheme = zero
62
- end
63
+ def pzm(plus_zero_minus)
64
+ pzm?(@data, plus_zero_minus)
63
65
  self
64
66
  end
65
67
 
66
- def pzm?(judge, plus, zero, minus)
68
+ def pzm?(judge, plus_zero_minus)
67
69
  judge = judge.to_f
68
70
  if judge > 0.0
69
- @color_scheme = plus
71
+ @color_scheme = plus_zero_minus[0]
70
72
  elsif judge < 0.0
71
- @color_scheme = minus
73
+ @color_scheme = plus_zero_minus[2]
72
74
  else
73
- @color_scheme = zero
75
+ @color_scheme = plus_zero_minus[1]
74
76
  end
75
77
  self
76
78
  end
@@ -123,67 +125,99 @@ require 'date'
123
125
  end
124
126
  alias show to_s
125
127
 
126
- def to_tag(tag="div")
127
- case @color_scheme
128
- when :hide
129
- content = ''
130
- else
131
- result = "<#{tag} class=\"#{@color_scheme}\">#{to_string}</#{tag}>"
128
+ def to_tag(tag="div", klass=nil)
129
+ klass = @color_scheme unless klass
130
+ if klass
131
+ result = "<#{tag} class=\"#{klass}\">#{to_string}</#{tag}>"
132
+ else
133
+ result = "<#{tag}>#{to_string}</#{tag}>"
132
134
  end
135
+ result
133
136
  end
134
137
 
135
- def to_div
136
- to_tag("div")
138
+ def to_div(klass=nil)
139
+ klass = @color_scheme unless klass
140
+ to_tag("div", klass)
137
141
  end
138
142
 
139
- def to_span
140
- to_tag("span")
143
+ def to_span(klass=nil)
144
+ klass = @color_scheme unless klass
145
+ to_tag("span", klass)
141
146
  end
142
147
 
143
- def to_td
144
- to_tag("td")
148
+ def to_td(klass=nil)
149
+ klass = @color_scheme unless klass
150
+ to_tag("td", klass)
145
151
  end
146
152
 
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)
153
+ def self.commify_string(num, thousand_sparator=',', decimal_separator='.', decimal=0)
161
154
  int, frac = *num.split(".")
162
- int = int.gsub(/(\d)(?=\d{3}+$)/, '\\1,')
163
- int << "." << frac if frac
155
+ int = int.gsub(/(\d)(?=\d{3}+$)/, "\\1#{thousand_sparator}")
156
+ if decimal > 0
157
+ int << "#{decimal_separator}"
158
+ #if frac
159
+ frac = "#{frac}000000"
160
+ int << frac[0..(decimal-1)]
161
+ #else
162
+ # ""
163
+ #end
164
+ end
164
165
  int
165
166
  end
166
167
 
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)
168
+ def self.commify_value(data, thousand_sparator=',', decimal_separator='.', decimal=0 )
169
+ case data.class.to_s
170
+ when 'Float'
171
+ return Formatting.commify_string(data.to_s, thousand_sparator, decimal_separator, decimal)
172
+ when 'Integer'
173
+ return Formatting.commify_string(data.to_s, thousand_sparator, decimal_separator, decimal)
171
174
  when 'String'
172
- return Formatting.euro_commify_string(num)
175
+ return Formatting.commify_string(data, thousand_sparator, decimal_separator, decimal)
173
176
  when 'NilClass'
174
- return "0"
177
+ if decimal == 0
178
+ return "0"
179
+ else
180
+ res = "0#{decimal_separator}00000000000"
181
+ return res[0..(decimal+1)]
182
+ end
175
183
  else
176
184
  return "#{num.class}"
177
185
  end
178
186
  end
179
187
 
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
188
+ def self.commify(data)
189
+ Formatting.commify_value(data, ',', '.', 2)
190
+ end
191
+
192
+ def self.euro_commify(data)
193
+ Formatting.commify_value(data, '.', ',', 2)
194
+ end
195
+
196
+ def self.commify0(data)
197
+ Formatting.commify_value(data, ',', '.', 0)
198
+ end
199
+
200
+ def self.euro_commify0(data)
201
+ Formatting.commify_value(data, '.', ',', 0)
202
+ end
203
+
204
+ def self.commify4(data)
205
+ Formatting.commify_value(data, ',', '.', 4)
206
+ end
207
+
208
+ def self.euro_commify4(data)
209
+ Formatting.commify_value(data, '.', ',', 4)
210
+ end
211
+
212
+ def self.japanese_yen(data)
213
+ "¥ " + Formatting.commify_value(data, ',', '.', 0)
214
+ end
215
+
216
+ def self.euro(data)
217
+ "€" + Formatting.commify_value(data, '.', ',', 2)
185
218
  end
186
219
 
220
+
187
221
  end
188
222
 
189
223
  class NilClass
@@ -1,3 +1,3 @@
1
1
  module HiroFormat
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # require "hiro_format" # you need gem install hiro_format
4
+ require "./lib/hiro_format/formatting"
5
+ require "./lib/hiro_format/coloring"
6
+
7
+ FIELD_LIST = [
8
+ {:title => "ID", :key => :id, :format => nil},
9
+ {:title => "Name", :key => :name, :format => :none, :color => "blue_class"},
10
+ {:title => "BirthDay", :key => :bday, :format => :obvious_date, },
11
+ {:title => "Assets", :key => :assets, :format => :euro, :pzm => ["green_class", "blue_class", "red_class"]},
12
+ ]
13
+ # @records = MyFriend.all
14
+ # For test without database engine
15
+ @records = [
16
+ {:id => 1, :name => "John", :bday => Date.parse('2000-01-02'), :assets => '1000000'},
17
+ {:id => 2, :name => "Kevin", :bday => Date.parse('2000-04-12'), :assets => '-10000'},
18
+ ]
19
+ puts "<table>"
20
+ puts "<tr>"
21
+ FIELD_LIST.each do |field|
22
+ puts "<th>#{field[:title]}</th>"
23
+ end
24
+ puts "</tr>"
25
+ @records.each do |my_friend|
26
+ puts "<tr>"
27
+ FIELD_LIST.each do |field|
28
+ if field[:pzm]
29
+ puts my_friend[field[:key]].formatting(field[:format]).pzm(field[:pzm]).to_td
30
+ else
31
+ puts my_friend[field[:key]].formatting(field[:format]).color(field[:color]).to_td
32
+ end
33
+ end
34
+ puts "</tr>"
35
+ end
36
+ puts "</table>"
@@ -15,11 +15,26 @@ require "hiro_format/formatting"
15
15
  require "hiro_format/coloring"
16
16
  # require "pry" # gem install pry pry-byebug
17
17
 
18
- value = 123456.78
19
- print ":commify #{value.class}: "
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
18
+ values = [1234567890.1234, 0]
19
+ values.each do |value|
20
+ puts "Value: #{value}"
21
+ print ":commify: "
22
+ puts value.formatting(:commify).color(:blue).to_s
23
+ print ":euro_commify: "
24
+ puts value.formatting(:euro_commify).color(:red).to_s
25
+ print ":commify0: "
26
+ puts value.formatting(:commify0).color(:blue).to_s
27
+ print ":euro_commify0: "
28
+ puts value.formatting(:euro_commify0).color(:red).to_s
29
+ print ":commify4: "
30
+ puts value.formatting(:commify4).color(:blue).to_s
31
+ print ":euro_commify4: "
32
+ puts value.formatting(:euro_commify4).color(:red).to_s
33
+ end
34
+
35
+ puts "12345".formatting(:japanese_yen).color(:green).to_s
36
+ puts "12345".formatting(:euro).color(:green).to_s
37
+
23
38
  puts Formatting.new(Date.today, :us_date).color(:magenta_marker).to_s
24
39
  puts Date.today.formatting(:euro_date).color(:reverse).to_s
25
40
  puts Date.today.formatting(:machine_date).color(:cyan_bold).to_s
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.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiro Utsumi
@@ -46,18 +46,20 @@ extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
48
  - ".gitignore"
49
+ - Formatting.md
49
50
  - Gemfile
50
51
  - LICENSE
51
52
  - README.md
52
53
  - Rakefile
53
54
  - bin/console
54
55
  - bin/setup
55
- - formatting_samples.rb
56
56
  - hiro_format.gemspec
57
57
  - lib/hiro_format.rb
58
58
  - lib/hiro_format/coloring.rb
59
59
  - lib/hiro_format/formatting.rb
60
60
  - lib/hiro_format/version.rb
61
+ - sample_active_record.rb
62
+ - sample_formatting.rb
61
63
  homepage: https://github.com/gambaldia/hiro_format.git
62
64
  licenses: []
63
65
  metadata: {}