coerce 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README +28 -0
  2. data/Rakefile +374 -0
  3. data/coerce.gemspec +29 -0
  4. data/lib/coerce.rb +194 -0
  5. metadata +70 -0
data/README ADDED
@@ -0,0 +1,28 @@
1
+ NAME
2
+ coerce
3
+
4
+ DESCRIPTION
5
+ a ruby library full of common cast/coercion operations
6
+
7
+ INSTALL
8
+ gem install coerce
9
+
10
+ USAGE
11
+
12
+ :~/git/coerce $ irb -r lib/coerce.rb
13
+
14
+ ruby-1.8.7-p330 :001 > Coerce.integer('42')
15
+ => 42
16
+
17
+ ruby-1.8.7-p330 :002 > Coerce.number('42.0')
18
+ => 42.0
19
+
20
+ ruby-1.8.7-p330 :003 > Coerce.time('yesterday')
21
+ => Wed Aug 24 12:00:00 -0600 2011
22
+
23
+ ruby-1.8.7-p330 :004 > Coerce.list_of_times('yesterday, today')
24
+ => [Wed Aug 24 12:00:00 -0600 2011, Thu Aug 25 19:30:00 -0600 2011]
25
+
26
+ ruby-1.8.7-p330 :005 > Coerce.list_of_floats("42, 4.2\n\n 420")
27
+ => [42.0, 4.2, 420.0]
28
+
@@ -0,0 +1,374 @@
1
+ This.rubyforge_project = 'codeforpeople'
2
+ This.author = "Ara T. Howard"
3
+ This.email = "ara.t.howard@gmail.com"
4
+ This.homepage = "https://github.com/ahoward/#{ This.lib }"
5
+
6
+ task :license do
7
+ open('LICENSE', 'w'){|fd| fd.puts "same as ruby's"}
8
+ end
9
+
10
+ task :default do
11
+ puts((Rake::Task.tasks.map{|task| task.name.gsub(/::/,':')} - ['default']).sort)
12
+ end
13
+
14
+ task :test do
15
+ run_tests!
16
+ end
17
+
18
+ namespace :test do
19
+ task(:unit){ run_tests!(:unit) }
20
+ task(:functional){ run_tests!(:functional) }
21
+ task(:integration){ run_tests!(:integration) }
22
+ end
23
+
24
+ def run_tests!(which = nil)
25
+ which ||= '**'
26
+ test_dir = File.join(This.dir, "test")
27
+ test_glob ||= File.join(test_dir, "#{ which }/**_test.rb")
28
+ test_rbs = Dir.glob(test_glob).sort
29
+
30
+ div = ('=' * 119)
31
+ line = ('-' * 119)
32
+
33
+ test_rbs.each_with_index do |test_rb, index|
34
+ testno = index + 1
35
+ command = "#{ This.ruby } -I ./lib -I ./test/lib #{ test_rb }"
36
+
37
+ puts
38
+ say(div, :color => :cyan, :bold => true)
39
+ say("@#{ testno } => ", :bold => true, :method => :print)
40
+ say(command, :color => :cyan, :bold => true)
41
+ say(line, :color => :cyan, :bold => true)
42
+
43
+ system(command)
44
+
45
+ say(line, :color => :cyan, :bold => true)
46
+
47
+ status = $?.exitstatus
48
+
49
+ if status.zero?
50
+ say("@#{ testno } <= ", :bold => true, :color => :white, :method => :print)
51
+ say("SUCCESS", :color => :green, :bold => true)
52
+ else
53
+ say("@#{ testno } <= ", :bold => true, :color => :white, :method => :print)
54
+ say("FAILURE", :color => :red, :bold => true)
55
+ end
56
+ say(line, :color => :cyan, :bold => true)
57
+
58
+ exit(status) unless status.zero?
59
+ end
60
+ end
61
+
62
+
63
+ task :gemspec do
64
+ ignore_extensions = ['git', 'svn', 'tmp', /sw./, 'bak', 'gem']
65
+ ignore_directories = ['pkg']
66
+ ignore_files = ['test/log', 'a.rb']
67
+
68
+ shiteless =
69
+ lambda do |list|
70
+ list.delete_if do |entry|
71
+ next unless test(?e, entry)
72
+ extension = File.basename(entry).split(%r/[.]/).last
73
+ ignore_extensions.any?{|ext| ext === extension}
74
+ end
75
+ list.delete_if do |entry|
76
+ next unless test(?d, entry)
77
+ dirname = File.expand_path(entry)
78
+ ignore_directories.any?{|dir| File.expand_path(dir) == dirname}
79
+ end
80
+ list.delete_if do |entry|
81
+ next unless test(?f, entry)
82
+ filename = File.expand_path(entry)
83
+ ignore_files.any?{|file| File.expand_path(file) == filename}
84
+ end
85
+ end
86
+
87
+ lib = This.lib
88
+ object = This.object
89
+ version = This.version
90
+ files = shiteless[Dir::glob("**/**")]
91
+ executables = shiteless[Dir::glob("bin/*")].map{|exe| File.basename(exe)}
92
+ #has_rdoc = true #File.exist?('doc')
93
+ test_files = "test/#{ lib }.rb" if File.file?("test/#{ lib }.rb")
94
+ summary = object.respond_to?(:summary) ? object.summary : "summary: #{ lib } kicks the ass"
95
+ description = object.respond_to?(:description) ? object.description : "description: #{ lib } kicks the ass"
96
+
97
+ if This.extensions.nil?
98
+ This.extensions = []
99
+ extensions = This.extensions
100
+ %w( Makefile configure extconf.rb ).each do |ext|
101
+ extensions << ext if File.exists?(ext)
102
+ end
103
+ end
104
+ extensions = [extensions].flatten.compact
105
+
106
+ template =
107
+ if test(?e, 'gemspec.erb')
108
+ Template{ IO.read('gemspec.erb') }
109
+ else
110
+ Template {
111
+ <<-__
112
+ ## #{ lib }.gemspec
113
+ #
114
+
115
+ Gem::Specification::new do |spec|
116
+ spec.name = #{ lib.inspect }
117
+ spec.version = #{ version.inspect }
118
+ spec.platform = Gem::Platform::RUBY
119
+ spec.summary = #{ lib.inspect }
120
+ spec.description = #{ description.inspect }
121
+
122
+ spec.files =\n#{ files.sort.pretty_inspect }
123
+ spec.executables = #{ executables.inspect }
124
+
125
+ spec.require_path = "lib"
126
+
127
+ spec.test_files = #{ test_files.inspect }
128
+
129
+ ### spec.add_dependency 'lib', '>= version'
130
+ #### spec.add_dependency 'map'
131
+
132
+ spec.extensions.push(*#{ extensions.inspect })
133
+
134
+ spec.rubyforge_project = #{ This.rubyforge_project.inspect }
135
+ spec.author = #{ This.author.inspect }
136
+ spec.email = #{ This.email.inspect }
137
+ spec.homepage = #{ This.homepage.inspect }
138
+ end
139
+ __
140
+ }
141
+ end
142
+
143
+ Fu.mkdir_p(This.pkgdir)
144
+ gemspec = "#{ lib }.gemspec"
145
+ open(gemspec, "w"){|fd| fd.puts(template)}
146
+ This.gemspec = gemspec
147
+ end
148
+
149
+ task :gem => [:clean, :gemspec] do
150
+ Fu.mkdir_p(This.pkgdir)
151
+ before = Dir['*.gem']
152
+ cmd = "gem build #{ This.gemspec }"
153
+ `#{ cmd }`
154
+ after = Dir['*.gem']
155
+ gem = ((after - before).first || after.first) or abort('no gem!')
156
+ Fu.mv(gem, This.pkgdir)
157
+ This.gem = File.join(This.pkgdir, File.basename(gem))
158
+ end
159
+
160
+ task :readme do
161
+ samples = ''
162
+ prompt = '~ > '
163
+ lib = This.lib
164
+ version = This.version
165
+
166
+ Dir['sample*/*'].sort.each do |sample|
167
+ samples << "\n" << " <========< #{ sample } >========>" << "\n\n"
168
+
169
+ cmd = "cat #{ sample }"
170
+ samples << Util.indent(prompt + cmd, 2) << "\n\n"
171
+ samples << Util.indent(`#{ cmd }`, 4) << "\n"
172
+
173
+ cmd = "ruby #{ sample }"
174
+ samples << Util.indent(prompt + cmd, 2) << "\n\n"
175
+
176
+ cmd = "ruby -e'STDOUT.sync=true; exec %(ruby -I ./lib #{ sample })'"
177
+ samples << Util.indent(`#{ cmd } 2>&1`, 4) << "\n"
178
+ end
179
+
180
+ template =
181
+ if test(?e, 'readme.erb')
182
+ Template{ IO.read('readme.erb') }
183
+ else
184
+ Template {
185
+ <<-__
186
+ NAME
187
+ #{ lib }
188
+
189
+ DESCRIPTION
190
+
191
+ INSTALL
192
+ gem install #{ lib }
193
+
194
+ SAMPLES
195
+ #{ samples }
196
+ __
197
+ }
198
+ end
199
+
200
+ open("README", "w"){|fd| fd.puts template}
201
+ end
202
+
203
+
204
+ task :clean do
205
+ Dir[File.join(This.pkgdir, '**/**')].each{|entry| Fu.rm_rf(entry)}
206
+ end
207
+
208
+
209
+ task :release => [:clean, :gemspec, :gem] do
210
+ gems = Dir[File.join(This.pkgdir, '*.gem')].flatten
211
+ raise "which one? : #{ gems.inspect }" if gems.size > 1
212
+ raise "no gems?" if gems.size < 1
213
+
214
+ cmd = "gem push #{ This.gem }"
215
+ puts cmd
216
+ puts
217
+ system(cmd)
218
+ abort("cmd(#{ cmd }) failed with (#{ $?.inspect })") unless $?.exitstatus.zero?
219
+
220
+ cmd = "rubyforge login && rubyforge add_release #{ This.rubyforge_project } #{ This.lib } #{ This.version } #{ This.gem }"
221
+ puts cmd
222
+ puts
223
+ system(cmd)
224
+ abort("cmd(#{ cmd }) failed with (#{ $?.inspect })") unless $?.exitstatus.zero?
225
+ end
226
+
227
+
228
+
229
+
230
+
231
+ BEGIN {
232
+ # support for this rakefile
233
+ #
234
+ $VERBOSE = nil
235
+
236
+ require 'ostruct'
237
+ require 'erb'
238
+ require 'fileutils'
239
+ require 'rbconfig'
240
+ require 'pp'
241
+
242
+ # fu shortcut
243
+ #
244
+ Fu = FileUtils
245
+
246
+ # cache a bunch of stuff about this rakefile/environment
247
+ #
248
+ This = OpenStruct.new
249
+
250
+ This.file = File.expand_path(__FILE__)
251
+ This.dir = File.dirname(This.file)
252
+ This.pkgdir = File.join(This.dir, 'pkg')
253
+
254
+ # grok lib
255
+ #
256
+ lib = ENV['LIB']
257
+ unless lib
258
+ lib = File.basename(Dir.pwd).sub(/[-].*$/, '')
259
+ end
260
+ This.lib = lib
261
+
262
+ # grok version
263
+ #
264
+ version = ENV['VERSION']
265
+ unless version
266
+ require "./lib/#{ This.lib }"
267
+ This.name = lib.capitalize
268
+ This.object = eval(This.name)
269
+ version = This.object.send(:version)
270
+ end
271
+ This.version = version
272
+
273
+ # we need to know the name of the lib an it's version
274
+ #
275
+ abort('no lib') unless This.lib
276
+ abort('no version') unless This.version
277
+
278
+ # discover full path to this ruby executable
279
+ #
280
+ c = Config::CONFIG
281
+ bindir = c["bindir"] || c['BINDIR']
282
+ ruby_install_name = c['ruby_install_name'] || c['RUBY_INSTALL_NAME'] || 'ruby'
283
+ ruby_ext = c['EXEEXT'] || ''
284
+ ruby = File.join(bindir, (ruby_install_name + ruby_ext))
285
+ This.ruby = ruby
286
+
287
+ # some utils
288
+ #
289
+ module Util
290
+ def indent(s, n = 2)
291
+ s = unindent(s)
292
+ ws = ' ' * n
293
+ s.gsub(%r/^/, ws)
294
+ end
295
+
296
+ def unindent(s)
297
+ indent = nil
298
+ s.each_line do |line|
299
+ next if line =~ %r/^\s*$/
300
+ indent = line[%r/^\s*/] and break
301
+ end
302
+ indent ? s.gsub(%r/^#{ indent }/, "") : s
303
+ end
304
+ extend self
305
+ end
306
+
307
+ # template support
308
+ #
309
+ class Template
310
+ def initialize(&block)
311
+ @block = block
312
+ @template = block.call.to_s
313
+ end
314
+ def expand(b=nil)
315
+ ERB.new(Util.unindent(@template)).result((b||@block).binding)
316
+ end
317
+ alias_method 'to_s', 'expand'
318
+ end
319
+ def Template(*args, &block) Template.new(*args, &block) end
320
+
321
+ # colored console output support
322
+ #
323
+ This.ansi = {
324
+ :clear => "\e[0m",
325
+ :reset => "\e[0m",
326
+ :erase_line => "\e[K",
327
+ :erase_char => "\e[P",
328
+ :bold => "\e[1m",
329
+ :dark => "\e[2m",
330
+ :underline => "\e[4m",
331
+ :underscore => "\e[4m",
332
+ :blink => "\e[5m",
333
+ :reverse => "\e[7m",
334
+ :concealed => "\e[8m",
335
+ :black => "\e[30m",
336
+ :red => "\e[31m",
337
+ :green => "\e[32m",
338
+ :yellow => "\e[33m",
339
+ :blue => "\e[34m",
340
+ :magenta => "\e[35m",
341
+ :cyan => "\e[36m",
342
+ :white => "\e[37m",
343
+ :on_black => "\e[40m",
344
+ :on_red => "\e[41m",
345
+ :on_green => "\e[42m",
346
+ :on_yellow => "\e[43m",
347
+ :on_blue => "\e[44m",
348
+ :on_magenta => "\e[45m",
349
+ :on_cyan => "\e[46m",
350
+ :on_white => "\e[47m"
351
+ }
352
+ def say(phrase, *args)
353
+ options = args.last.is_a?(Hash) ? args.pop : {}
354
+ options[:color] = args.shift.to_s.to_sym unless args.empty?
355
+ keys = options.keys
356
+ keys.each{|key| options[key.to_s.to_sym] = options.delete(key)}
357
+
358
+ color = options[:color]
359
+ bold = options.has_key?(:bold)
360
+
361
+ parts = [phrase]
362
+ parts.unshift(This.ansi[color]) if color
363
+ parts.unshift(This.ansi[:bold]) if bold
364
+ parts.push(This.ansi[:clear]) if parts.size > 1
365
+
366
+ method = options[:method] || :puts
367
+
368
+ Kernel.send(method, parts.join)
369
+ end
370
+
371
+ # always run out of the project dir
372
+ #
373
+ Dir.chdir(This.dir)
374
+ }
@@ -0,0 +1,29 @@
1
+ ## coerce.gemspec
2
+ #
3
+
4
+ Gem::Specification::new do |spec|
5
+ spec.name = "coerce"
6
+ spec.version = "0.0.1"
7
+ spec.platform = Gem::Platform::RUBY
8
+ spec.summary = "coerce"
9
+ spec.description = "description: coerce kicks the ass"
10
+
11
+ spec.files =
12
+ ["README", "Rakefile", "coerce.gemspec", "lib", "lib/coerce.rb"]
13
+
14
+ spec.executables = []
15
+
16
+ spec.require_path = "lib"
17
+
18
+ spec.test_files = nil
19
+
20
+ ### spec.add_dependency 'lib', '>= version'
21
+ #### spec.add_dependency 'map'
22
+
23
+ spec.extensions.push(*[])
24
+
25
+ spec.rubyforge_project = "codeforpeople"
26
+ spec.author = "Ara T. Howard"
27
+ spec.email = "ara.t.howard@gmail.com"
28
+ spec.homepage = "https://github.com/ahoward/coerce"
29
+ end
@@ -0,0 +1,194 @@
1
+ module Coerce
2
+ ## built-in
3
+ #
4
+ require 'uri'
5
+ require 'time'
6
+ require 'date'
7
+ require 'pathname'
8
+ require 'chronic'
9
+
10
+ ## version
11
+ #
12
+ Coerce::Version = '0.0.1'
13
+
14
+ def self.version
15
+ Coerce::Version
16
+ end
17
+
18
+ ## dependencies
19
+ #
20
+ def self.dependencies
21
+ {
22
+ 'chronic' => [ 'chronic' , '~> 0.6.2' ]
23
+ }
24
+ end
25
+
26
+ begin
27
+ require 'rubygems'
28
+ rescue LoadError
29
+ nil
30
+ end
31
+
32
+ if defined?(gem)
33
+ self.dependencies.each do |lib, dependency|
34
+ gem(*dependency)
35
+ require(lib)
36
+ end
37
+ end
38
+
39
+ ##
40
+ #
41
+ def self.export m
42
+ module_function m
43
+ public m
44
+ end
45
+
46
+ List = []
47
+
48
+ def self.coerce m, &b
49
+ define_method m, &b
50
+ export m
51
+ List << m.to_s
52
+ end
53
+
54
+ coerce :boolean do |obj|
55
+ case obj.to_s
56
+ when %r/^(true|t|1)$/
57
+ true
58
+ when %r/^(false|f|0)$/
59
+ false
60
+ else
61
+ !!obj
62
+ end
63
+ end
64
+
65
+ coerce :integer do |obj|
66
+ Float(obj).to_i
67
+ end
68
+
69
+ coerce :float do |obj|
70
+ Float obj
71
+ end
72
+
73
+ coerce :number do |obj|
74
+ Float obj rescue Integer obj
75
+ end
76
+
77
+ coerce :string do |obj|
78
+ String obj
79
+ end
80
+
81
+ coerce :symbol do |obj|
82
+ String(obj).to_sym
83
+ end
84
+
85
+ coerce :uri do |obj|
86
+ ::URI.parse obj.to_s
87
+ end
88
+
89
+ coerce :url do |obj|
90
+ ::URI.parse(obj.to_s).to_s
91
+ end
92
+
93
+ coerce :time do |obj|
94
+ ::Chronic.parse(obj.to_s)
95
+ end
96
+
97
+ coerce :date do |obj|
98
+ begin
99
+ ::Date.parse(::Chronic.parse(obj.to_s).to_s)
100
+ rescue
101
+ ::Date.parse(obj.to_s)
102
+ end
103
+ end
104
+
105
+ coerce :pathname do |obj|
106
+ Pathname.new(obj.to_s)
107
+ end
108
+
109
+ coerce :path do |obj|
110
+ File.expand_path(obj.to_s)
111
+ end
112
+
113
+ coerce :input do |obj|
114
+ case obj.to_s
115
+ when '-'
116
+ io = STDIN.dup
117
+ io.fattr(:path){ '/dev/stdin' }
118
+ io
119
+ else
120
+ io = open(obj.to_s, 'r+')
121
+ at_exit{ io.close }
122
+ io
123
+ end
124
+ end
125
+
126
+ coerce :output do |obj|
127
+ case obj.to_s
128
+ when '-'
129
+ io = STDOUT.dup
130
+ io.fattr(:path){ '/dev/stdout' }
131
+ io
132
+ else
133
+ io = open(obj.to_s, 'w+')
134
+ at_exit{ io.close }
135
+ io
136
+ end
137
+ end
138
+
139
+ coerce :slug do |obj|
140
+ string = [obj].flatten.compact.join('-')
141
+ words = string.to_s.scan(%r/\w+/)
142
+ words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
143
+ words.delete_if{|word| word.nil? or word.strip.empty?}
144
+ String(words.join('-').downcase)
145
+ end
146
+
147
+ coerce :list do |*objs|
148
+ [*objs].flatten.join(',').split(/[\n,]/).map{|item| item.strip}.delete_if{|item| item.strip.empty?}
149
+ end
150
+
151
+ # add list_of_xxx methods
152
+ #
153
+ List.dup.each do |type|
154
+ next if type.to_s =~ %r/list/
155
+ %W" list_of_#{ type } list_of_#{ type }s ".each do |m|
156
+ define_method m do |*objs|
157
+ list(*objs).map{|obj| send type, obj}
158
+ end
159
+ export m
160
+ List << m
161
+ end
162
+ end
163
+
164
+ # add list_of_xxx_from_file
165
+ #
166
+ List.dup.each do |type|
167
+ next if type.to_s =~ %r/list/
168
+ %W" list_of_#{ type }_from_file list_of_#{ type }s_from_file ".each do |m|
169
+ define_method m do |*args|
170
+ buf = nil
171
+ if args.size == 1 and args.first.respond_to?(:read)
172
+ buf = args.first.read
173
+ else
174
+ open(*args){|io| buf = io.read}
175
+ end
176
+ send(m.sub(/_from_file/, ''), buf)
177
+ end
178
+ export m
179
+ List << m
180
+ end
181
+ end
182
+
183
+ def self.[] sym
184
+ prefix = sym.to_s.downcase.to_sym
185
+ candidates = List.select{|m| m =~ %r/^#{ prefix }/i}
186
+ m = candidates.shift
187
+ raise ArgumentError, "unsupported coercion: #{ sym.inspect } (#{ List.join ',' })" unless
188
+ m
189
+ raise ArgumentError, "ambiguous coercion: #{ sym.inspect } (#{ List.join ',' })" unless
190
+ candidates.empty? or m.to_s == sym.to_s
191
+ this = self
192
+ lambda{|obj| method(m).call obj}
193
+ end
194
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: coerce
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Ara T. Howard
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-08-25 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: "description: coerce kicks the ass"
23
+ email: ara.t.howard@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - README
32
+ - Rakefile
33
+ - coerce.gemspec
34
+ - lib/coerce.rb
35
+ has_rdoc: true
36
+ homepage: https://github.com/ahoward/coerce
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ hash: 3
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project: codeforpeople
65
+ rubygems_version: 1.4.2
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: coerce
69
+ test_files: []
70
+