hiro_format 0.0.1

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: 9f96bf9fe44818eab0773d61c7d5105ef411b150
4
+ data.tar.gz: d1f57179c47e28cc096253e3c7b8445c1c33a7b0
5
+ SHA512:
6
+ metadata.gz: 6ff10774dcccc2a73b6292e854b70f6995b9c5f7c1e285ebc411313eccc133efae548a7cb2af21ee352caf443edff8e78e122772ec6d21e3159039ad77864dec
7
+ data.tar.gz: ee9aa9b3df5fa6938bc0f387dfaa24095f6a83b5566cf8b18e96299bebda71bd571a882e10e2224c1160d4a60c378ec656ee9ed572a387d2cefc4107c896d77b
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in hiro_tools.gemspec
4
+ gemspec
5
+ gem "pry"
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # HiroFormat
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/hiro_format`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'hiro_format'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install hiro_format
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hiro_tools.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hiro_format"
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
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "hiro_format/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hiro_format"
8
+ spec.version = HiroFormat::VERSION
9
+ spec.authors = ["Hiro Utsumi"]
10
+ spec.email = ["gambaldia@gmail.com"]
11
+
12
+ spec.summary = %q{HiroFormat formats ruby variables into various format. It has coloring capability, too.}
13
+ spec.description = %q{.}
14
+ spec.homepage = "https://github.com/gambaldia/hiro_format.git"
15
+ #spec.executables = ['', '']
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.15"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ end
@@ -0,0 +1,107 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # HiroTools Formatter - Coloring
4
+ #
5
+ # Hiro Utsumi (Github: Gambaldia)- Zolder Belgium
6
+ #
7
+ # 2017-07-22 Start
8
+ #
9
+
10
+ class AnsiColoring
11
+
12
+ COLOR_CODES = {
13
+ :black => 0,
14
+ :red => 1,
15
+ :green => 2,
16
+ :yellow => 3,
17
+ :blue => 4,
18
+ :magenta => 5,
19
+ :cyan => 6,
20
+ :white => 7,
21
+ :default => 9
22
+ }.freeze
23
+ # Light color = +60
24
+ # Foreground = +30
25
+ # Background = +40
26
+ # DISPLAY_MODES = {:default => 0,:bold => 1,:fainted => 2,:italic => 3,}.freeze
27
+ COLOR_SCHEMES = {
28
+ :black => "0;30;0",
29
+ :black_red => "1;31;40",
30
+ :black_yellow => "1;33;40",
31
+ :black_cyan => "1;36;40",
32
+ :reverse => "0;37;40",
33
+ :red => "0;31;49",
34
+ :red_bold => "1;31;49",
35
+ :red_back => "0;37;41",
36
+ :red_marker => "0;30;101",
37
+ :green => "0;32;49",
38
+ :green_bold => "1;32;49",
39
+ :green_back => "0;37;42",
40
+ :green_marker => "0;30;102",
41
+ :yellow => "0;33;49",
42
+ :yellow_bold => "1;33;49",
43
+ :yellow_back => "0;30;43",
44
+ :yellow_marker => "0;30;103",
45
+ :blue => "0;34;49",
46
+ :blue_bold => "1;34;49",
47
+ :blue_back => "0;37;44",
48
+ :blue_marker => "0;37;104",
49
+ :magenta => "0;35;49",
50
+ :magenta_bold => "1;35;49",
51
+ :magenta_back => "0;37;45",
52
+ :magenta_marker => "0;30;105",
53
+ :cyan => "0;36;49",
54
+ :cyan_bold => "1;36;49",
55
+ :cyan_back => "0;37;46",
56
+ :cyan_marker => "0;30;106",
57
+ :gray => "0;37;49",
58
+ :gray_bold => "1;37;49",
59
+ :gray_back => "0;30;47",
60
+ :gray_marker => "0;30;47",
61
+ }.freeze
62
+
63
+ def initialize(data, color_scheme)
64
+ @data = data
65
+ @color_scheme = nil
66
+ end
67
+
68
+ def to_s
69
+ AnsiColoring.colorize(@data, @color_scheme)
70
+ end
71
+
72
+ def self.colorize(data, color_scheme=nil)
73
+ case color_scheme
74
+ when nil?
75
+ data
76
+ when :hide
77
+ ''
78
+ else
79
+ "\033[#{COLOR_SCHEMES[color_scheme]}m#{data}\033[0m"
80
+ end
81
+ end
82
+
83
+ def self.show_list
84
+ puts "Color schemes list:"
85
+ COLOR_SCHEMES.each do |key, scheme|
86
+ puts AnsiColoring.colorize(key, key)
87
+ end
88
+ end
89
+
90
+ end
91
+
92
+ class NilClass
93
+ def color(color_scheme)
94
+ AnsiColoring.new(self, color_scheme)
95
+ end
96
+ end
97
+
98
+ class String
99
+ def color(color_scheme)
100
+ AnsiColoring.new(self, color_scheme)
101
+ end
102
+ end
103
+
104
+
105
+ if (__FILE__ == $0)
106
+ Coloring.show_list
107
+ end
@@ -0,0 +1,225 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # HiroTools Formatter
4
+ #
5
+ # Hiro Utsumi (Github: Gambaldia)- Zolder Belgium
6
+ #
7
+ # 2017-07-22 Start
8
+ #
9
+ APP_ROOT = File.dirname(__FILE__)
10
+ $LOAD_PATH << "#{APP_ROOT}"
11
+ require 'date'
12
+ require 'coloring'
13
+
14
+ class Formatting
15
+ YOUBI = %w[日 月 火 水 木 金 土]
16
+ FORMATTER_LIST = {
17
+ :date => {:time_format => "%Y-%m-%d", :applicable => [Date, DateTime], :rest => "0000-00-00" },
18
+ :jp_date => {:time_format => "%Y-%m-%d", :applicable => [Date, DateTime], :rest => "0000-00-00" },
19
+ :us_date => {:time_format => "%m-%d-%Y", :applicable => [Date, DateTime], :rest => "0000-00-00" },
20
+ :euro_date => {:time_format => "%d-%m-%Y", :applicable => [Date, DateTime], :rest => "0000-00-00" },
21
+ :obvious_date => {:time_format => "%d-%b-%Y", :applicable => [Date, DateTime], :rest => "0000-00-00" },
22
+ :machine_date => {:time_format => "%Y%m%d", :applicable => [Date, DateTime], :rest => "00000000" },
23
+
24
+ :datewday => {:time_format => "%Y-%m-%d (%a)", :applicable => [Date, DateTime], :rest => "0000-00-00 (---)" },
25
+ :jp_datewday => {:time_format => "%Y-%m-%d (%a)", :applicable => [Date, DateTime], :rest => "0000-00-00 (---)" },
26
+ :us_datewday => {:time_format => "%m-%d-%Y (%a)", :applicable => [Date, DateTime], :rest => "00-00-0000 (---)" },
27
+ :euro_datewday => {:time_format => "%d-%m-%Y (%a)", :applicable => [Date, DateTime], :rest => "00-00-0000 (---)" },
28
+
29
+ :datetime => {:time_format => "%Y-%m-%d %H:%M", :applicable => [Date, DateTime, Time], :rest => "0000-00-00" },
30
+ :datetimesecond => {:time_format => "%Y-%m-%d %H:%M%s", :applicable => [Date, DateTime, Time], :rest => "0000-00-00" },
31
+ :jp_datetime => {:time_format => "%Y-%m-%d %H:%M", :applicable => [Date, DateTime, Time], :rest => "0000-00-00" },
32
+ :jp_datetimesecond => {:time_format => "%Y-%m-%d %H:%M:%s", :applicable => [Date, DateTime, Time], :rest => "0000-00-00 00:00:00" },
33
+ :us_datetime => {:time_format => "%m-%d-%Y %H:%M", :applicable => [Date, DateTime, Time], :rest => "00-00-0000 00:00" },
34
+ :us_datetimesecond => {:time_format => "%m-%d-%Y %H:%M:%s", :applicable => [Date, DateTime, Time], :rest => "00-00-0000 00:00:00" },
35
+ :euro_datetime => {:time_format => "%d-%m-%Y %H:%M", :applicable => [Date, DateTime, Time], :rest => "00-00-0000 00:00" },
36
+ :euro_datetimesecond => {:time_format => "%d-%m-%Y %H:%M:%s", :applicable => [Date, DateTime, Time], :rest => "0000-00-00 00:00:00" },
37
+ :machine_datetime => {:time_format => "%Y%m%d%H%M", :applicable => [Date, DateTime], :rest => "000000000000" },
38
+
39
+ :digit6 =>{:format => "%06d", :applicable => [Integer, Float], :rest => "000000"},
40
+ :digit2 =>{:format => "%02d", :applicable => [Integer, Float], :rest => "00"},
41
+
42
+ :func =>{:function => "", :applicable => [], :rest => ""},
43
+ }.freeze
44
+
45
+ def initialize(data, formatting_option=nil)
46
+ @data = data
47
+ @formatting_option = formatting_option
48
+ end
49
+
50
+ def color(color_scheme)
51
+ @color_scheme = color_scheme
52
+ self
53
+ end
54
+
55
+ def pzm?(plus, zero, minus)
56
+ judge = @data.to_f
57
+ if judge > 0.0
58
+ @color_scheme = plus
59
+ elsif judge < 0.0
60
+ @color_scheme = minus
61
+ else
62
+ @color_scheme = zero
63
+ end
64
+ self
65
+ end
66
+
67
+ def pzm(judge, plus, zero, minus)
68
+ judge = judge.to_f
69
+ if judge > 0.0
70
+ @color_scheme = plus
71
+ elsif judge < 0.0
72
+ @color_scheme = minus
73
+ else
74
+ @color_scheme = zero
75
+ end
76
+ self
77
+ end
78
+
79
+ def yesno?(yes, no)
80
+ if @data
81
+ @color_scheme = yes
82
+ else
83
+ @color_scheme = no
84
+ end
85
+ self
86
+ end
87
+
88
+
89
+ def yesno(judge, yes, no)
90
+ if judge
91
+ @color_scheme = yes
92
+ else
93
+ @color_scheme = no
94
+ end
95
+ self
96
+ end
97
+
98
+ def to_string
99
+ if @formatting_option.nil?
100
+ #result = ""
101
+ elsif recipe = FORMATTER_LIST[@formatting_option]
102
+ if recipe[:applicable].include?(@data.class)
103
+ if recipe[:time_format]
104
+ result = @data.strftime(recipe[:time_format])
105
+ elsif recipe[:format]
106
+ result = recipe[:format] % @data
107
+ elsif recipe[:function]
108
+ Formatting.send(recipe[:function], @data)
109
+ end
110
+ else
111
+ result = recipe[:rest]
112
+ end
113
+ end
114
+ result || @data.to_s
115
+ end
116
+
117
+ def to_s
118
+ result = to_string
119
+ if @color_scheme
120
+ result = AnsiColoring.colorize(result, @color_scheme)
121
+ end
122
+ #binding.pry
123
+ result
124
+ end
125
+ alias show to_s
126
+
127
+ def to_tag(tag="div")
128
+ case @color_scheme
129
+ when :hide
130
+ content = ''
131
+ else
132
+ result = "<#{tag} class=\"#{@color_scheme}\">#{to_string}</#{tag}>"
133
+ end
134
+ end
135
+
136
+ def to_div
137
+ to_tag("div")
138
+ end
139
+
140
+ def to_span
141
+ to_tag("span")
142
+ end
143
+
144
+ def to_td
145
+ to_tag("td")
146
+ end
147
+
148
+ def self.commify(num)
149
+ int, frac = *num.split(".")
150
+ int = int.gsub(/(\d)(?=\d{3}+$)/, '\\1,')
151
+ int << "." << frac if frac
152
+ int
153
+ end
154
+
155
+ end
156
+
157
+ class NilClass
158
+ def formatting(option=nil)
159
+ Formatting.new(self, option)
160
+ end
161
+ end
162
+
163
+ class String
164
+ def formatting(option=nil)
165
+ Formatting.new(self, option)
166
+ end
167
+
168
+ def commify
169
+ self.gsub(/(\d)(?=\d{3}+$)/, '\\1,')
170
+ end
171
+ end
172
+
173
+ class Date
174
+ def formatting(option=nil)
175
+ Formatting.new(self, option)
176
+ end
177
+ end
178
+
179
+ class Time
180
+ def formatting(option=nil)
181
+ Formatting.new(self, option)
182
+ end
183
+ end
184
+
185
+ class DateTime
186
+ def formatting(option=nil)
187
+ Formatting.new(self, option)
188
+ end
189
+ end
190
+
191
+ class Integer
192
+ def formatting(option=nil)
193
+ Formatting.new(self, option)
194
+ end
195
+
196
+ def commify
197
+ self.to_s.commify
198
+ end
199
+ end
200
+
201
+ class Float
202
+ def formatting(option=nil)
203
+ Formatting.new(self, option)
204
+ end
205
+
206
+ def commify
207
+ Formatting.commify(self.to_s)
208
+ end
209
+ end
210
+
211
+ class BigDecimal
212
+ def formatting(option=nil)
213
+ Formatting.new(self, option)
214
+ end
215
+ end
216
+
217
+ if (__FILE__ == $0)
218
+ $verbose = true
219
+ [0, 1234, 12345.67, "test", Date.today, Time.now].each do |value|
220
+ [:html, :commify, :jp_datewday].each do |fmt|
221
+ puts "#{value}->#{fmt}: #{value.formatting(fmt).to_s}"
222
+ end
223
+ end
224
+ AnsiColoring.show_list
225
+ end
@@ -0,0 +1,3 @@
1
+ module HiroFormat
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ require "hiro_format/version"
2
+ require "hiro_format/formatting"
3
+ require "hiro_format/coloring"
4
+
5
+ module HiroFormat
6
+ def self.version
7
+ puts VERSION
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hiro_format
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Hiro Utsumi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-23 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
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
+ description: "."
42
+ email:
43
+ - gambaldia@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - README.md
51
+ - Rakefile
52
+ - bin/console
53
+ - bin/setup
54
+ - hiro_format.gemspec
55
+ - lib/hiro_format.rb
56
+ - lib/hiro_format/coloring.rb
57
+ - lib/hiro_format/formatting.rb
58
+ - lib/hiro_format/version.rb
59
+ homepage: https://github.com/gambaldia/hiro_format.git
60
+ licenses: []
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.6.12
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: HiroFormat formats ruby variables into various format. It has coloring capability,
82
+ too.
83
+ test_files: []