hiro_format 0.0.4 → 0.0.5

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: fbd05ad43abfdead030270847cfbb1ee175059d3
4
- data.tar.gz: b131ba35f8d03322e97bab7d2f8a49e92ae55356
3
+ metadata.gz: 4e37f34a492d81b11e2216f932308cff72e0000d
4
+ data.tar.gz: 5e69fb4900b13dad9a9f7283a1ce127f5e3f74f9
5
5
  SHA512:
6
- metadata.gz: 9f55048df4a8dcaa3c73f7ba03740ce68e55f672ab0295831759bec7591bd390d0fa93ce84e6c20feb5bb3761b371697ac32aa8eacf8e7ad412914f15ce77ec9
7
- data.tar.gz: ad68d0cf118f665c65e1f4bd4921d84e8ab91bbfd7fb1ed004637eb441331bc96a0ba1a506217d71d3ffd9668668fc1c56b6da9a9088e5ac174ff953cffd451f
6
+ metadata.gz: bb0b687811881752479d2b3b70aaed7f1084a0b6ab761bd95dd9f3493fab473061f2b713f434b8bc7102f10fa1751450b5edc6b6d9ebcf95f8323a86ecedb407
7
+ data.tar.gz: 54744fe771614c99f3988ce0e82e88238b88eb806ce7cb2f435a0e89d3d6d11f4edf5ed14a6c78deb0b5b28ad9cc8573960fc7d4ce60adacc95509cfb9f8da7f
data/README.md CHANGED
@@ -60,32 +60,72 @@ See formatting_sample.rb for more examples.
60
60
  ## Usage hint for Active Record / Sequel users
61
61
 
62
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
- ]
63
+ # For Gem users
64
+ require "hiro_format"
65
+ # For cloner
66
+ #require "./lib/hiro_format/formatting"
67
+ #require "./lib/hiro_format/coloring"
68
+
69
+ # Model class
70
+ class MyFriend # < Sequel::Model # < ActiveRecord::Base
71
+ RECORDS = [
72
+ {:id => 1, :name => "John", :bday => Date.parse('1989-01-02'), :assets => 1000000, :gender => 1},
73
+ {:id => 2, :name => "Kevin", :bday => Date.parse('1991-04-12'), :assets => -10000, :gender => 0},
74
+ {:id => 3, :name => "Wendy", :bday => Date.parse('1990-05-12'), :assets => 0, :gender => 2},
75
+ ]
76
+ GENDERS = {
77
+ 0 => 'Other',
78
+ 1 => 'Male',
79
+ 2 => 'Female',
80
+ }
81
+ FIELD_LIST = [
82
+ {:title => "ID", :key => :id, :callback => :edit_button},
83
+ {:title => "Name", :key => :name, :format => :none, :color => "blue_class"},
84
+ {:title => "Gender", :key => :gender, :lookup => GENDERS, },
85
+ {:title => "BirthDay", :key => :bday, :format => :obvious_date, },
86
+ {:title => "Assets", :key => :assets, :format => :euro, :pzm => ["green_class", "blue_class", "red_class"]},
87
+ ]
88
+ attr_reader :data
89
+ def initialize(data)
90
+ @data = data
91
+ end
92
+
93
+ def edit_button
94
+ "<input type=\"button\" class=\"btn-sm\" value=\"Edit\" onClick=\"location.href='/controller/edit/#{@data[:id]}'\">"
95
+ end
96
+
97
+ def [](key)
98
+ @data[key]
99
+ end
100
+
101
+ def self.all
102
+ result = []
103
+ RECORDS.each do |record|
104
+ result << self.new(record)
105
+ end
106
+ result
107
+ end
108
+ end
109
+
110
+ # Controller
111
+ @records = MyFriend.all
112
+ @field_list = MyFriend::FIELD_LIST
113
+
114
+ # View can be independent from ModelClass
79
115
  puts "<table>"
80
116
  puts "<tr>"
81
- FIELD_LIST.each do |field|
117
+ @field_list.each do |field|
82
118
  puts "<th>#{field[:title]}</th>"
83
119
  end
84
120
  puts "</tr>"
85
121
  @records.each do |my_friend|
86
122
  puts "<tr>"
87
- FIELD_LIST.each do |field|
88
- if field[:pzm]
123
+ @field_list.each do |field|
124
+ if field[:callback]
125
+ puts "<td>" + my_friend.send(field[:callback]) + "</td>"
126
+ elsif field[:lookup]
127
+ puts "<td>" + field[:lookup][my_friend[field[:key]]] + "</td>"
128
+ elsif field[:pzm]
89
129
  puts my_friend[field[:key]].formatting(field[:format]).pzm(field[:pzm]).to_td
90
130
  else
91
131
  puts my_friend[field[:key]].formatting(field[:format]).color(field[:color]).to_td
@@ -9,7 +9,18 @@
9
9
  require 'date'
10
10
 
11
11
  class Formatting
12
- YOUBI = %w[日 月 火 水 木 金 土]
12
+ if RUBY_VERSION >= '2.4.0'
13
+ FORMATTER_LIST_SUB = {
14
+ :digit6 => {:format => "%06d", :applicable => [Integer, Float], :rest => "000000"},
15
+ :digit2 => {:format => "%02d", :applicable => [Integer, Float], :rest => "00"},
16
+ }.freeze
17
+ else
18
+ FORMATTER_LIST_SUB = {
19
+ :digit6 => {:format => "%06d", :applicable => [Fixnum, Integer, Float], :rest => "000000"},
20
+ :digit2 => {:format => "%02d", :applicable => [Fixnum, Integer, Float], :rest => "00"},
21
+ }.freeze
22
+ end
23
+
13
24
  FORMATTER_LIST = {
14
25
  :date => {:time_format => "%Y-%m-%d", :applicable => [Date, DateTime], :rest => "0000-00-00" },
15
26
  :jp_date => {:time_format => "%Y-%m-%d", :applicable => [Date, DateTime], :rest => "0000-00-00" },
@@ -33,10 +44,6 @@ require 'date'
33
44
  :euro_datetimesecond => {:time_format => "%d-%m-%Y %H:%M:%s", :applicable => [Date, DateTime, Time], :rest => "0000-00-00 00:00:00" },
34
45
  :machine_datetime => {:time_format => "%Y%m%d%H%M", :applicable => [Date, DateTime], :rest => "000000000000" },
35
46
 
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"},
39
-
40
47
  :commify => {:function => "commify", :applicable => [Integer, Float, String, NilClass], :rest => ""},
41
48
  :euro_commify => {:function => "euro_commify", :applicable => [Integer, Float, String, NilClass], :rest => ""},
42
49
  :commify0 => {:function => "commify0", :applicable => [Integer, Float, String, NilClass], :rest => ""},
@@ -49,12 +56,21 @@ require 'date'
49
56
 
50
57
  :hide => {:format => "", :applicable => [], :rest => ""},
51
58
  }.freeze
59
+ YOUBI = %w[日 月 火 水 木 金 土]
60
+
61
+ # Not yet used
62
+ @@custom_formatter = {}
52
63
 
53
64
  def initialize(data, formatting_option=nil)
54
65
  @data = data
55
66
  @formatting_option = formatting_option
56
67
  end
57
68
 
69
+ def lookup(lookup_hash)
70
+ @lookup = lookup_hash
71
+ self
72
+ end
73
+
58
74
  def color(color_scheme)
59
75
  @color_scheme = color_scheme
60
76
  self
@@ -77,17 +93,11 @@ require 'date'
77
93
  self
78
94
  end
79
95
 
80
- def yesno?(yes, no)
81
- if @data
82
- @color_scheme = yes
83
- else
84
- @color_scheme = no
85
- end
86
- self
96
+ def yesno(yes, no)
97
+ yesno?(data, yes, no)
87
98
  end
88
99
 
89
-
90
- def yesno(judge, yes, no)
100
+ def yesno?(judge, yes, no)
91
101
  if judge
92
102
  @color_scheme = yes
93
103
  else
@@ -96,10 +106,17 @@ require 'date'
96
106
  self
97
107
  end
98
108
 
109
+ def get_recipe
110
+ FORMATTER_LIST[@formatting_option] || FORMATTER_LIST_SUB[@formatting_option]
111
+ #@@custom_formatter[@formatting_option] || FORMATTER_LIST[@formatting_option] || FORMATTER_LIST_SUB[@formatting_option]
112
+ end
113
+
99
114
  def to_string
100
- if @formatting_option.nil?
115
+ if @lookup && (@lookup.is_a?(Hash) || @lookup.is_a?(Array))
116
+ result = @lookup[@data]
117
+ elsif @formatting_option.nil?
101
118
  #result = @data.to_s
102
- elsif recipe = FORMATTER_LIST[@formatting_option]
119
+ elsif recipe = get_recipe
103
120
  # puts @data.class
104
121
  if recipe[:applicable].include?(@data.class)
105
122
  if recipe[:time_format]
@@ -217,6 +234,10 @@ require 'date'
217
234
  "€" + Formatting.commify_value(data, '.', ',', 2)
218
235
  end
219
236
 
237
+ # Not yet used
238
+ def self.add_custom_formatter(new_formatter)
239
+ @@custom_formatter << new_formatter
240
+ end
220
241
 
221
242
  end
222
243
 
@@ -1,3 +1,3 @@
1
1
  module HiroFormat
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -1,31 +1,71 @@
1
1
  #!/usr/bin/env ruby
2
2
  #
3
- # require "hiro_format" # you need gem install hiro_format
3
+ # For Gem users
4
+ #require "hiro_format"
5
+ # For cloner
4
6
  require "./lib/hiro_format/formatting"
5
7
  require "./lib/hiro_format/coloring"
6
8
 
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
- ]
9
+ # Model class
10
+ class MyFriend # < Sequel::Model # < ActiveRecord::Base
11
+ RECORDS = [
12
+ {:id => 1, :name => "John", :bday => Date.parse('1989-01-02'), :assets => 1000000, :gender => 1},
13
+ {:id => 2, :name => "Kevin", :bday => Date.parse('1991-04-12'), :assets => -10000, :gender => 0},
14
+ {:id => 3, :name => "Wendy", :bday => Date.parse('1990-05-12'), :assets => 0, :gender => 2},
15
+ ]
16
+ GENDERS = {
17
+ 0 => 'Other',
18
+ 1 => 'Male',
19
+ 2 => 'Female',
20
+ }
21
+ FIELD_LIST = [
22
+ {:title => "ID", :key => :id, :callback => :edit_button},
23
+ {:title => "Name", :key => :name, :format => :none, :color => "blue_class"},
24
+ {:title => "Gender", :key => :gender, :lookup => GENDERS, },
25
+ {:title => "BirthDay", :key => :bday, :format => :obvious_date, },
26
+ {:title => "Assets", :key => :assets, :format => :euro, :pzm => ["green_class", "blue_class", "red_class"]},
27
+ ]
28
+ attr_reader :data
29
+ def initialize(data)
30
+ @data = data
31
+ end
32
+
33
+ def edit_button
34
+ "<input type=\"button\" class=\"btn-sm\" value=\"Edit\" onClick=\"location.href='/controller/edit/#{@data[:id]}'\">"
35
+ end
36
+
37
+ def [](key)
38
+ @data[key]
39
+ end
40
+
41
+ def self.all
42
+ result = []
43
+ RECORDS.each do |record|
44
+ result << self.new(record)
45
+ end
46
+ result
47
+ end
48
+ end
49
+
50
+ # Controller
51
+ @records = MyFriend.all
52
+ @field_list = MyFriend::FIELD_LIST
53
+
54
+ # View can be independent from ModelClass
19
55
  puts "<table>"
20
56
  puts "<tr>"
21
- FIELD_LIST.each do |field|
57
+ @field_list.each do |field|
22
58
  puts "<th>#{field[:title]}</th>"
23
59
  end
24
60
  puts "</tr>"
25
61
  @records.each do |my_friend|
26
62
  puts "<tr>"
27
- FIELD_LIST.each do |field|
28
- if field[:pzm]
63
+ @field_list.each do |field|
64
+ if field[:callback]
65
+ puts "<td>" + my_friend.send(field[:callback]) + "</td>"
66
+ elsif field[:lookup]
67
+ puts my_friend[field[:key]].formatting.lookup(field[:lookup]).to_td
68
+ elsif field[:pzm]
29
69
  puts my_friend[field[:key]].formatting(field[:format]).pzm(field[:pzm]).to_td
30
70
  else
31
71
  puts my_friend[field[:key]].formatting(field[:format]).color(field[:color]).to_td
data/sample_formatting.rb CHANGED
@@ -41,7 +41,7 @@ puts Date.today.formatting(:machine_date).color(:cyan_bold).to_s
41
41
  puts "#pzm (Plus, Zero, Minus) can show in different color depending on the value passed."
42
42
  [-1, 0, 1].each do |n|
43
43
  print "#{n.class}: "
44
- puts n.formatting(:digit6).pzm(:reverse, :red_marker, :magenta_marker).to_s
44
+ puts n.formatting(:digit6).pzm([:reverse, :red_marker, :magenta_marker]).to_s
45
45
  end
46
46
  puts "Formatting helper for HTML, you set your color in CSS (Cyan Marker)".color(:cyan_marker).to_s
47
47
  puts Formatting.new(Date.today, :us_date).color("magenta").to_span # <span class="magenta">12-24-2017</span>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hiro_format
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiro Utsumi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-07-24 00:00:00.000000000 Z
11
+ date: 2017-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler