Class: Formatting
- Inherits:
-
Object
- Object
- Formatting
- Defined in:
- lib/hiro_format/formatting.rb
Constant Summary collapse
- FORMATTER_LIST_SUB =
{ :digit6 => {:format => "%06d", :applicable => [Fixnum, Integer, Float], :rest => "000000"}, :digit2 => {:format => "%02d", :applicable => [Fixnum, Integer, Float], :rest => "00"}, }.freeze
- FORMATTER_LIST =
{ :date => {:time_format => "%Y-%m-%d", :applicable => [Date, DateTime], :rest => "0000-00-00" }, :jp_date => {:time_format => "%Y-%m-%d", :applicable => [Date, DateTime], :rest => "0000-00-00" }, :us_date => {:time_format => "%m-%d-%Y", :applicable => [Date, DateTime], :rest => "0000-00-00" }, :euro_date => {:time_format => "%d-%m-%Y", :applicable => [Date, DateTime], :rest => "0000-00-00" }, :obvious_date => {:time_format => "%d-%b-%Y", :applicable => [Date, DateTime], :rest => "0000-00-00" }, :machine_date => {:time_format => "%Y%m%d", :applicable => [Date, DateTime], :rest => "00000000" }, :datewday => {:time_format => "%Y-%m-%d (%a)", :applicable => [Date, DateTime], :rest => "0000-00-00 (---)" }, :jp_datewday => {:time_format => "%Y-%m-%d (%a)", :applicable => [Date, DateTime], :rest => "0000-00-00 (---)" }, :us_datewday => {:time_format => "%m-%d-%Y (%a)", :applicable => [Date, DateTime], :rest => "00-00-0000 (---)" }, :euro_datewday => {:time_format => "%d-%m-%Y (%a)", :applicable => [Date, DateTime], :rest => "00-00-0000 (---)" }, :datetime => {:time_format => "%Y-%m-%d %H:%M", :applicable => [Date, DateTime, Time], :rest => "0000-00-00" }, :datetimesecond => {:time_format => "%Y-%m-%d %H:%M%s", :applicable => [Date, DateTime, Time], :rest => "0000-00-00" }, :jp_datetime => {:time_format => "%Y-%m-%d %H:%M", :applicable => [Date, DateTime, Time], :rest => "0000-00-00" }, :jp_datetimesecond => {:time_format => "%Y-%m-%d %H:%M:%s", :applicable => [Date, DateTime, Time], :rest => "0000-00-00 00:00:00" }, :us_datetime => {:time_format => "%m-%d-%Y %H:%M", :applicable => [Date, DateTime, Time], :rest => "00-00-0000 00:00" }, :us_datetimesecond => {:time_format => "%m-%d-%Y %H:%M:%s", :applicable => [Date, DateTime, Time], :rest => "00-00-0000 00:00:00" }, :euro_datetime => {:time_format => "%d-%m-%Y %H:%M", :applicable => [Date, DateTime, Time], :rest => "00-00-0000 00:00" }, :euro_datetimesecond => {:time_format => "%d-%m-%Y %H:%M:%s", :applicable => [Date, DateTime, Time], :rest => "0000-00-00 00:00:00" }, :machine_datetime => {:time_format => "%Y%m%d%H%M", :applicable => [Date, DateTime], :rest => "000000000000" }, :commify => {:function => "commify", :applicable => [Integer, Float, String, NilClass], :rest => ""}, :euro_commify => {:function => "euro_commify", :applicable => [Integer, Float, String, NilClass], :rest => ""}, :commify0 => {:function => "commify0", :applicable => [Integer, Float, String, NilClass], :rest => ""}, :euro_commify0 => {:function => "euro_commify0", :applicable => [Integer, Float, String, NilClass], :rest => ""}, :commify4 => {:function => "commify4", :applicable => [Integer, Float, String, NilClass], :rest => ""}, :euro_commify4 => {:function => "euro_commify4", :applicable => [Integer, Float, String, NilClass], :rest => ""}, :japanese_yen => {:function => "japanese_yen", :applicable => [Integer, Float, String, NilClass], :rest => ""}, :euro => {:function => "euro", :applicable => [Integer, Float, String, NilClass], :rest => ""}, :hide => {:format => "", :applicable => [], :rest => ""}, }.freeze
- YOUBI =
%w[日 月 火 水 木 金 土]
- @@custom_formatter =
Not yet used
{}
Class Method Summary collapse
-
.add_custom_formatter(new_formatter) ⇒ Object
Not yet used.
- .commify(data) ⇒ Object
- .commify0(data) ⇒ Object
- .commify4(data) ⇒ Object
- .commify_string(num, thousand_sparator = ',', decimal_separator = '.', decimal = 0) ⇒ Object
- .commify_value(data, thousand_sparator = ',', decimal_separator = '.', decimal = 0) ⇒ Object
- .euro(data) ⇒ Object
- .euro_commify(data) ⇒ Object
- .euro_commify0(data) ⇒ Object
- .euro_commify4(data) ⇒ Object
- .japanese_yen(data) ⇒ Object
Instance Method Summary collapse
- #color(color_scheme) ⇒ Object
- #get_recipe ⇒ Object
-
#initialize(data, formatting_option = nil) ⇒ Formatting
constructor
A new instance of Formatting.
- #lookup(lookup_hash) ⇒ Object
- #pzm(plus_zero_minus) ⇒ Object
- #pzm?(judge, plus_zero_minus) ⇒ Boolean
- #to_div(klass = nil) ⇒ Object
- #to_s ⇒ Object (also: #show)
- #to_span(klass = nil) ⇒ Object
- #to_string ⇒ Object
- #to_tag(tag = "div", klass = nil) ⇒ Object
- #to_td(klass = nil) ⇒ Object
- #yesno(yes, no) ⇒ Object
- #yesno?(judge, yes, no) ⇒ Boolean
Constructor Details
#initialize(data, formatting_option = nil) ⇒ Formatting
Returns a new instance of Formatting.
64 65 66 67 |
# File 'lib/hiro_format/formatting.rb', line 64 def initialize(data, formatting_option=nil) @data = data @formatting_option = formatting_option end |
Class Method Details
.add_custom_formatter(new_formatter) ⇒ Object
Not yet used
238 239 240 |
# File 'lib/hiro_format/formatting.rb', line 238 def self.add_custom_formatter(new_formatter) @@custom_formatter << new_formatter end |
.commify(data) ⇒ Object
205 206 207 |
# File 'lib/hiro_format/formatting.rb', line 205 def self.commify(data) Formatting.commify_value(data, ',', '.', 2) end |
.commify0(data) ⇒ Object
213 214 215 |
# File 'lib/hiro_format/formatting.rb', line 213 def self.commify0(data) Formatting.commify_value(data, ',', '.', 0) end |
.commify4(data) ⇒ Object
221 222 223 |
# File 'lib/hiro_format/formatting.rb', line 221 def self.commify4(data) Formatting.commify_value(data, ',', '.', 4) end |
.commify_string(num, thousand_sparator = ',', decimal_separator = '.', decimal = 0) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/hiro_format/formatting.rb', line 170 def self.commify_string(num, thousand_sparator=',', decimal_separator='.', decimal=0) int, frac = *num.split(".") int = int.gsub(/(\d)(?=\d{3}+$)/, "\\1#{thousand_sparator}") if decimal > 0 int << "#{decimal_separator}" #if frac frac = "#{frac}000000" int << frac[0..(decimal-1)] #else # "" #end end int end |
.commify_value(data, thousand_sparator = ',', decimal_separator = '.', decimal = 0) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/hiro_format/formatting.rb', line 185 def self.commify_value(data, thousand_sparator=',', decimal_separator='.', decimal=0 ) case data.class.to_s when 'Float' return Formatting.commify_string(data.to_s, thousand_sparator, decimal_separator, decimal) when 'Integer' return Formatting.commify_string(data.to_s, thousand_sparator, decimal_separator, decimal) when 'String' return Formatting.commify_string(data, thousand_sparator, decimal_separator, decimal) when 'NilClass' if decimal == 0 return "0" else res = "0#{decimal_separator}00000000000" return res[0..(decimal+1)] end else return "#{num.class}" end end |
.euro(data) ⇒ Object
233 234 235 |
# File 'lib/hiro_format/formatting.rb', line 233 def self.euro(data) "€" + Formatting.commify_value(data, '.', ',', 2) end |
.euro_commify(data) ⇒ Object
209 210 211 |
# File 'lib/hiro_format/formatting.rb', line 209 def self.euro_commify(data) Formatting.commify_value(data, '.', ',', 2) end |
.euro_commify0(data) ⇒ Object
217 218 219 |
# File 'lib/hiro_format/formatting.rb', line 217 def self.euro_commify0(data) Formatting.commify_value(data, '.', ',', 0) end |
.euro_commify4(data) ⇒ Object
225 226 227 |
# File 'lib/hiro_format/formatting.rb', line 225 def self.euro_commify4(data) Formatting.commify_value(data, '.', ',', 4) end |
.japanese_yen(data) ⇒ Object
229 230 231 |
# File 'lib/hiro_format/formatting.rb', line 229 def self.japanese_yen(data) "¥ " + Formatting.commify_value(data, ',', '.', 0) end |
Instance Method Details
#color(color_scheme) ⇒ Object
74 75 76 77 |
# File 'lib/hiro_format/formatting.rb', line 74 def color(color_scheme) @color_scheme = color_scheme self end |
#get_recipe ⇒ Object
109 110 111 112 |
# File 'lib/hiro_format/formatting.rb', line 109 def get_recipe FORMATTER_LIST[@formatting_option] || FORMATTER_LIST_SUB[@formatting_option] #@@custom_formatter[@formatting_option] || FORMATTER_LIST[@formatting_option] || FORMATTER_LIST_SUB[@formatting_option] end |
#lookup(lookup_hash) ⇒ Object
69 70 71 72 |
# File 'lib/hiro_format/formatting.rb', line 69 def lookup(lookup_hash) @lookup = lookup_hash self end |
#pzm(plus_zero_minus) ⇒ Object
79 80 81 82 |
# File 'lib/hiro_format/formatting.rb', line 79 def pzm(plus_zero_minus) pzm?(@data, plus_zero_minus) self end |
#pzm?(judge, plus_zero_minus) ⇒ Boolean
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/hiro_format/formatting.rb', line 84 def pzm?(judge, plus_zero_minus) judge = judge.to_f if judge > 0.0 @color_scheme = plus_zero_minus[0] elsif judge < 0.0 @color_scheme = plus_zero_minus[2] else @color_scheme = plus_zero_minus[1] end self end |
#to_div(klass = nil) ⇒ Object
155 156 157 158 |
# File 'lib/hiro_format/formatting.rb', line 155 def to_div(klass=nil) klass = @color_scheme unless klass to_tag("div", klass) end |
#to_s ⇒ Object Also known as: show
136 137 138 139 140 141 142 |
# File 'lib/hiro_format/formatting.rb', line 136 def to_s result = to_string if @color_scheme && AnsiColoring result = AnsiColoring.colorize(result, @color_scheme) end result end |
#to_span(klass = nil) ⇒ Object
160 161 162 163 |
# File 'lib/hiro_format/formatting.rb', line 160 def to_span(klass=nil) klass = @color_scheme unless klass to_tag("span", klass) end |
#to_string ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/hiro_format/formatting.rb', line 114 def to_string if @lookup && (@lookup.is_a?(Hash) || @lookup.is_a?(Array)) result = @lookup[@data] elsif @formatting_option.nil? #result = @data.to_s elsif recipe = get_recipe # puts @data.class if recipe[:applicable].include?(@data.class) if recipe[:time_format] result = @data.strftime(recipe[:time_format]) elsif recipe[:format] result = recipe[:format] % @data elsif recipe[:function] result = Formatting.send(recipe[:function], @data) end else result = recipe[:rest] end end result || @data.to_s end |
#to_tag(tag = "div", klass = nil) ⇒ Object
145 146 147 148 149 150 151 152 153 |
# File 'lib/hiro_format/formatting.rb', line 145 def to_tag(tag="div", klass=nil) klass = @color_scheme unless klass if klass result = "<#{tag} class=\"#{klass}\">#{to_string}</#{tag}>" else result = "<#{tag}>#{to_string}</#{tag}>" end result end |
#to_td(klass = nil) ⇒ Object
165 166 167 168 |
# File 'lib/hiro_format/formatting.rb', line 165 def to_td(klass=nil) klass = @color_scheme unless klass to_tag("td", klass) end |
#yesno(yes, no) ⇒ Object
96 97 98 |
# File 'lib/hiro_format/formatting.rb', line 96 def yesno(yes, no) yesno?(data, yes, no) end |
#yesno?(judge, yes, no) ⇒ Boolean
100 101 102 103 104 105 106 107 |
# File 'lib/hiro_format/formatting.rb', line 100 def yesno?(judge, yes, no) if judge @color_scheme = yes else @color_scheme = no end self end |