dao 7.0.0 → 8.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Rakefile +4 -5
- data/coerce-0.0.8/README +28 -0
- data/coerce-0.0.8/Rakefile +392 -0
- data/coerce-0.0.8/coerce.gemspec +31 -0
- data/coerce-0.0.8/lib/coerce.rb +210 -0
- data/dao.gemspec +25 -19
- data/lib/dao.rb +8 -15
- data/lib/dao/_lib.rb +6 -10
- data/lib/dao/active_record.rb +2 -2
- data/lib/dao/api/call.rb +6 -2
- data/lib/dao/coerce.rb +211 -0
- data/lib/dao/conducer.rb +6 -10
- data/lib/dao/errors2html.rb +1 -1
- data/lib/dao/support.rb +6 -17
- data/lib/dao/wrap.rb +259 -0
- data/test/conducer_test.rb +4 -4
- data/test/form_test.rb +2 -2
- data/test/validations_test.rb +2 -2
- data/wrap-1.5.2/README +57 -0
- data/wrap-1.5.2/Rakefile +394 -0
- data/wrap-1.5.2/lib/wrap.rb +295 -0
- data/wrap-1.5.2/test/testing.rb +195 -0
- data/wrap-1.5.2/test/wrap_test.rb +397 -0
- data/wrap-1.5.2/wrap.gemspec +38 -0
- metadata +26 -72
- data/notes/ara.txt +0 -15
@@ -0,0 +1,31 @@
|
|
1
|
+
## coerce.gemspec
|
2
|
+
#
|
3
|
+
|
4
|
+
Gem::Specification::new do |spec|
|
5
|
+
spec.name = "coerce"
|
6
|
+
spec.version = "0.0.8"
|
7
|
+
spec.platform = Gem::Platform::RUBY
|
8
|
+
spec.summary = "coerce"
|
9
|
+
spec.description = "description: coerce kicks the ass"
|
10
|
+
spec.license = "same as ruby's"
|
11
|
+
|
12
|
+
spec.files =
|
13
|
+
["README", "Rakefile", "coerce.gemspec", "lib", "lib/coerce.rb"]
|
14
|
+
|
15
|
+
spec.executables = []
|
16
|
+
|
17
|
+
spec.require_path = "lib"
|
18
|
+
|
19
|
+
spec.test_files = nil
|
20
|
+
|
21
|
+
|
22
|
+
spec.add_dependency(*["chronic", ">= 0.6.2"])
|
23
|
+
|
24
|
+
|
25
|
+
spec.extensions.push(*[])
|
26
|
+
|
27
|
+
spec.rubyforge_project = "codeforpeople"
|
28
|
+
spec.author = "Ara T. Howard"
|
29
|
+
spec.email = "ara.t.howard@gmail.com"
|
30
|
+
spec.homepage = "https://github.com/ahoward/coerce"
|
31
|
+
end
|
@@ -0,0 +1,210 @@
|
|
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.8'
|
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|yes|y|on)$/i
|
57
|
+
true
|
58
|
+
when %r/^(false|f|0|no|n|off)$/i
|
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
|
+
coerce :array do |*objs|
|
152
|
+
[*objs].flatten.join(',').split(/[\n,]/).map{|item| item.strip}.delete_if{|item| item.strip.empty?}
|
153
|
+
end
|
154
|
+
|
155
|
+
coerce :hash do |*objs|
|
156
|
+
list = Coerce.list(*objs)
|
157
|
+
hash = Hash.new
|
158
|
+
list.each do |pair|
|
159
|
+
k, v = pair.split(/[=:]+/, 2)
|
160
|
+
key = k.to_s.strip
|
161
|
+
val = v.to_s.strip
|
162
|
+
hash[key] = val
|
163
|
+
end
|
164
|
+
hash
|
165
|
+
end
|
166
|
+
|
167
|
+
# add list_of_xxx methods
|
168
|
+
#
|
169
|
+
List.dup.each do |type|
|
170
|
+
next if type.to_s =~ %r/list/
|
171
|
+
%W" list_of_#{ type } list_of_#{ type }s ".each do |m|
|
172
|
+
define_method m do |*objs|
|
173
|
+
list(*objs).map{|obj| send type, obj}
|
174
|
+
end
|
175
|
+
export m
|
176
|
+
List << m
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
# add list_of_xxx_from_file
|
181
|
+
#
|
182
|
+
List.dup.each do |type|
|
183
|
+
next if type.to_s =~ %r/list/
|
184
|
+
%W" list_of_#{ type }_from_file list_of_#{ type }s_from_file ".each do |m|
|
185
|
+
define_method m do |*args|
|
186
|
+
buf = nil
|
187
|
+
if args.size == 1 and args.first.respond_to?(:read)
|
188
|
+
buf = args.first.read
|
189
|
+
else
|
190
|
+
open(*args){|io| buf = io.read}
|
191
|
+
end
|
192
|
+
send(m.sub(/_from_file/, ''), buf)
|
193
|
+
end
|
194
|
+
export m
|
195
|
+
List << m
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
def self.[] sym
|
200
|
+
prefix = sym.to_s.downcase.to_sym
|
201
|
+
candidates = List.select{|m| m =~ %r/^#{ prefix }/i}
|
202
|
+
m = candidates.shift
|
203
|
+
raise ArgumentError, "unsupported coercion: #{ sym.inspect } (#{ List.join ',' })" unless
|
204
|
+
m
|
205
|
+
raise ArgumentError, "ambiguous coercion: #{ sym.inspect } (#{ List.join ',' })" unless
|
206
|
+
candidates.empty? or m.to_s == sym.to_s
|
207
|
+
this = self
|
208
|
+
lambda{|obj| method(m).call obj}
|
209
|
+
end
|
210
|
+
end
|
data/dao.gemspec
CHANGED
@@ -3,15 +3,21 @@
|
|
3
3
|
|
4
4
|
Gem::Specification::new do |spec|
|
5
5
|
spec.name = "dao"
|
6
|
-
spec.version = "
|
6
|
+
spec.version = "8.0.0"
|
7
7
|
spec.platform = Gem::Platform::RUBY
|
8
8
|
spec.summary = "dao"
|
9
9
|
spec.description = "presenter, conductor, api, and better form objects for you rails' pleasure"
|
10
|
-
spec.license = "
|
10
|
+
spec.license = "Ruby"
|
11
11
|
|
12
12
|
spec.files =
|
13
13
|
["README.md",
|
14
14
|
"Rakefile",
|
15
|
+
"coerce-0.0.8",
|
16
|
+
"coerce-0.0.8/README",
|
17
|
+
"coerce-0.0.8/Rakefile",
|
18
|
+
"coerce-0.0.8/coerce.gemspec",
|
19
|
+
"coerce-0.0.8/lib",
|
20
|
+
"coerce-0.0.8/lib/coerce.rb",
|
15
21
|
"dao.gemspec",
|
16
22
|
"lib",
|
17
23
|
"lib/dao",
|
@@ -27,6 +33,7 @@ Gem::Specification::new do |spec|
|
|
27
33
|
"lib/dao/api/modes.rb",
|
28
34
|
"lib/dao/api/routes.rb",
|
29
35
|
"lib/dao/blankslate.rb",
|
36
|
+
"lib/dao/coerce.rb",
|
30
37
|
"lib/dao/conducer",
|
31
38
|
"lib/dao/conducer.rb",
|
32
39
|
"lib/dao/conducer/active_model.rb",
|
@@ -85,8 +92,7 @@ Gem::Specification::new do |spec|
|
|
85
92
|
"lib/dao/validations/common.rb",
|
86
93
|
"lib/dao/validations/instance.rb",
|
87
94
|
"lib/dao/validations/validator.rb",
|
88
|
-
"
|
89
|
-
"notes/ara.txt",
|
95
|
+
"lib/dao/wrap.rb",
|
90
96
|
"public",
|
91
97
|
"public/system",
|
92
98
|
"public/system/uploads",
|
@@ -106,7 +112,16 @@ Gem::Specification::new do |spec|
|
|
106
112
|
"test/support_test.rb",
|
107
113
|
"test/test_helper.rb",
|
108
114
|
"test/util.rb",
|
109
|
-
"test/validations_test.rb"
|
115
|
+
"test/validations_test.rb",
|
116
|
+
"wrap-1.5.2",
|
117
|
+
"wrap-1.5.2/README",
|
118
|
+
"wrap-1.5.2/Rakefile",
|
119
|
+
"wrap-1.5.2/lib",
|
120
|
+
"wrap-1.5.2/lib/wrap.rb",
|
121
|
+
"wrap-1.5.2/test",
|
122
|
+
"wrap-1.5.2/test/testing.rb",
|
123
|
+
"wrap-1.5.2/test/wrap_test.rb",
|
124
|
+
"wrap-1.5.2/wrap.gemspec"]
|
110
125
|
|
111
126
|
spec.executables = []
|
112
127
|
|
@@ -115,28 +130,19 @@ Gem::Specification::new do |spec|
|
|
115
130
|
spec.test_files = nil
|
116
131
|
|
117
132
|
|
118
|
-
spec.add_dependency(*["rails", " ~>
|
133
|
+
spec.add_dependency(*["rails", " ~> 6.0"])
|
119
134
|
|
120
|
-
spec.add_dependency(*["map", " ~> 6.
|
135
|
+
spec.add_dependency(*["map", " ~> 6.6"])
|
121
136
|
|
122
|
-
spec.add_dependency(*["fattr", " ~> 2.
|
137
|
+
spec.add_dependency(*["fattr", " ~> 2.4"])
|
123
138
|
|
124
|
-
spec.add_dependency(*["
|
139
|
+
spec.add_dependency(*["tagz", " ~> 9.10"])
|
125
140
|
|
126
|
-
spec.add_dependency(*["
|
127
|
-
|
128
|
-
spec.add_dependency(*["multi_json", " ~> 1.0"])
|
129
|
-
|
130
|
-
spec.add_dependency(*["uuidtools", " ~> 2.1"])
|
131
|
-
|
132
|
-
spec.add_dependency(*["wrap", " ~> 1.5"])
|
133
|
-
|
134
|
-
spec.add_dependency(*["rails_current", " ~> 2.0"])
|
141
|
+
spec.add_dependency(*["rails_current", " ~> 2.2"])
|
135
142
|
|
136
143
|
|
137
144
|
spec.extensions.push(*[])
|
138
145
|
|
139
|
-
spec.rubyforge_project = "codeforpeople"
|
140
146
|
spec.author = "Ara T. Howard"
|
141
147
|
spec.email = "ara.t.howard@gmail.com"
|
142
148
|
spec.homepage = "https://github.com/ahoward/dao"
|
data/lib/dao.rb
CHANGED
@@ -7,17 +7,15 @@
|
|
7
7
|
require 'cgi'
|
8
8
|
require 'tmpdir'
|
9
9
|
require 'yaml'
|
10
|
+
require 'securerandom'
|
11
|
+
require 'json'
|
10
12
|
|
11
13
|
# dao libs
|
12
14
|
#
|
13
|
-
require
|
14
|
-
require
|
15
|
-
require
|
16
|
-
require
|
17
|
-
require "multi_json"
|
18
|
-
require "uuidtools"
|
19
|
-
require "wrap"
|
20
|
-
require "rails_current"
|
15
|
+
require 'map'
|
16
|
+
require 'fattr'
|
17
|
+
require 'tagz'
|
18
|
+
require 'rails_current'
|
21
19
|
|
22
20
|
#
|
23
21
|
require_relative 'dao/_lib.rb'
|
@@ -43,6 +41,8 @@
|
|
43
41
|
#
|
44
42
|
Dao.load %w[
|
45
43
|
blankslate.rb
|
44
|
+
coerce.rb
|
45
|
+
wrap.rb
|
46
46
|
instance_exec.rb
|
47
47
|
extractor.rb
|
48
48
|
exceptions.rb
|
@@ -77,10 +77,3 @@
|
|
77
77
|
conducer.rb
|
78
78
|
upload.rb
|
79
79
|
]
|
80
|
-
|
81
|
-
|
82
|
-
unless defined?(::UUIDTools::Config)
|
83
|
-
::UUIDTools.module_eval do
|
84
|
-
Config = ::RbConfig # shuts up warnings...
|
85
|
-
end
|
86
|
-
end
|
data/lib/dao/_lib.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Dao
|
2
|
-
Version = '
|
2
|
+
Version = '8.0.0' unless defined?(Version)
|
3
3
|
|
4
4
|
def version
|
5
5
|
Dao::Version
|
@@ -7,15 +7,11 @@ module Dao
|
|
7
7
|
|
8
8
|
def dependencies
|
9
9
|
{
|
10
|
-
'rails' => [ 'rails' , ' ~>
|
11
|
-
'map' => [ 'map' , ' ~> 6.
|
12
|
-
'fattr' => [ 'fattr' , ' ~> 2.
|
13
|
-
'
|
14
|
-
'
|
15
|
-
'multi_json' => [ 'multi_json' , ' ~> 1.0' ] ,
|
16
|
-
'uuidtools' => [ 'uuidtools' , ' ~> 2.1' ] ,
|
17
|
-
'wrap' => [ 'wrap' , ' ~> 1.5' ] ,
|
18
|
-
'rails_current' => [ 'rails_current' , ' ~> 2.0' ] ,
|
10
|
+
'rails' => [ 'rails' , ' ~> 6.0' ] ,
|
11
|
+
'map' => [ 'map' , ' ~> 6.6' ] ,
|
12
|
+
'fattr' => [ 'fattr' , ' ~> 2.4' ] ,
|
13
|
+
'tagz' => [ 'tagz' , ' ~> 9.10' ] ,
|
14
|
+
'rails_current' => [ 'rails_current' , ' ~> 2.2' ] ,
|
19
15
|
}
|
20
16
|
end
|
21
17
|
|
data/lib/dao/active_record.rb
CHANGED
data/lib/dao/api/call.rb
CHANGED
@@ -242,8 +242,12 @@ module Dao
|
|
242
242
|
|
243
243
|
# delgate some methods to the context
|
244
244
|
#
|
245
|
-
Context.attrs.each do |method|
|
246
|
-
|
245
|
+
(Context.attrs - %w[ status data ]).each do |method|
|
246
|
+
if method_defined?(method)
|
247
|
+
#p [method, instance_method(method).source_location].join(':')
|
248
|
+
remove_method(method)
|
249
|
+
end
|
250
|
+
|
247
251
|
module_eval <<-__, __FILE__, __LINE__
|
248
252
|
def #{ method }(*args)
|
249
253
|
context.send(#{ method.inspect }, *args)
|
data/lib/dao/coerce.rb
ADDED
@@ -0,0 +1,211 @@
|
|
1
|
+
module Dao
|
2
|
+
module Coerce
|
3
|
+
## built-in
|
4
|
+
#
|
5
|
+
require 'uri'
|
6
|
+
require 'time'
|
7
|
+
require 'date'
|
8
|
+
require 'pathname'
|
9
|
+
require 'chronic'
|
10
|
+
|
11
|
+
## version
|
12
|
+
#
|
13
|
+
Coerce::Version = '0.0.8'
|
14
|
+
|
15
|
+
def self.version
|
16
|
+
Coerce::Version
|
17
|
+
end
|
18
|
+
|
19
|
+
## dependencies
|
20
|
+
#
|
21
|
+
def self.dependencies
|
22
|
+
{
|
23
|
+
'chronic' => [ 'chronic' , '>= 0.6.2' ]
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rubygems'
|
29
|
+
rescue LoadError
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
|
33
|
+
if defined?(gem)
|
34
|
+
self.dependencies.each do |lib, dependency|
|
35
|
+
gem(*dependency)
|
36
|
+
require(lib)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
##
|
41
|
+
#
|
42
|
+
def self.export m
|
43
|
+
module_function m
|
44
|
+
public m
|
45
|
+
end
|
46
|
+
|
47
|
+
List = []
|
48
|
+
|
49
|
+
def self.coerce m, &b
|
50
|
+
define_method m, &b
|
51
|
+
export m
|
52
|
+
List << m.to_s
|
53
|
+
end
|
54
|
+
|
55
|
+
coerce :boolean do |obj|
|
56
|
+
case obj.to_s
|
57
|
+
when %r/^(true|t|1|yes|y|on)$/i
|
58
|
+
true
|
59
|
+
when %r/^(false|f|0|no|n|off)$/i
|
60
|
+
false
|
61
|
+
else
|
62
|
+
!!obj
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
coerce :integer do |obj|
|
67
|
+
Float(obj).to_i
|
68
|
+
end
|
69
|
+
|
70
|
+
coerce :float do |obj|
|
71
|
+
Float obj
|
72
|
+
end
|
73
|
+
|
74
|
+
coerce :number do |obj|
|
75
|
+
Float obj rescue Integer obj
|
76
|
+
end
|
77
|
+
|
78
|
+
coerce :string do |obj|
|
79
|
+
String obj
|
80
|
+
end
|
81
|
+
|
82
|
+
coerce :symbol do |obj|
|
83
|
+
String(obj).to_sym
|
84
|
+
end
|
85
|
+
|
86
|
+
coerce :uri do |obj|
|
87
|
+
::URI.parse obj.to_s
|
88
|
+
end
|
89
|
+
|
90
|
+
coerce :url do |obj|
|
91
|
+
::URI.parse(obj.to_s).to_s
|
92
|
+
end
|
93
|
+
|
94
|
+
coerce :time do |obj|
|
95
|
+
::Chronic.parse(obj.to_s)
|
96
|
+
end
|
97
|
+
|
98
|
+
coerce :date do |obj|
|
99
|
+
begin
|
100
|
+
::Date.parse(::Chronic.parse(obj.to_s).to_s)
|
101
|
+
rescue
|
102
|
+
::Date.parse(obj.to_s)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
coerce :pathname do |obj|
|
107
|
+
Pathname.new(obj.to_s)
|
108
|
+
end
|
109
|
+
|
110
|
+
coerce :path do |obj|
|
111
|
+
File.expand_path(obj.to_s)
|
112
|
+
end
|
113
|
+
|
114
|
+
coerce :input do |obj|
|
115
|
+
case obj.to_s
|
116
|
+
when '-'
|
117
|
+
io = STDIN.dup
|
118
|
+
io.fattr(:path){ '/dev/stdin' }
|
119
|
+
io
|
120
|
+
else
|
121
|
+
io = open(obj.to_s, 'r+')
|
122
|
+
at_exit{ io.close }
|
123
|
+
io
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
coerce :output do |obj|
|
128
|
+
case obj.to_s
|
129
|
+
when '-'
|
130
|
+
io = STDOUT.dup
|
131
|
+
io.fattr(:path){ '/dev/stdout' }
|
132
|
+
io
|
133
|
+
else
|
134
|
+
io = open(obj.to_s, 'w+')
|
135
|
+
at_exit{ io.close }
|
136
|
+
io
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
coerce :slug do |obj|
|
141
|
+
string = [obj].flatten.compact.join('-')
|
142
|
+
words = string.to_s.scan(%r/\w+/)
|
143
|
+
words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
|
144
|
+
words.delete_if{|word| word.nil? or word.strip.empty?}
|
145
|
+
String(words.join('-').downcase)
|
146
|
+
end
|
147
|
+
|
148
|
+
coerce :list do |*objs|
|
149
|
+
[*objs].flatten.join(',').split(/[\n,]/).map{|item| item.strip}.delete_if{|item| item.strip.empty?}
|
150
|
+
end
|
151
|
+
|
152
|
+
coerce :array do |*objs|
|
153
|
+
[*objs].flatten.join(',').split(/[\n,]/).map{|item| item.strip}.delete_if{|item| item.strip.empty?}
|
154
|
+
end
|
155
|
+
|
156
|
+
coerce :hash do |*objs|
|
157
|
+
list = Coerce.list(*objs)
|
158
|
+
hash = Hash.new
|
159
|
+
list.each do |pair|
|
160
|
+
k, v = pair.split(/[=:]+/, 2)
|
161
|
+
key = k.to_s.strip
|
162
|
+
val = v.to_s.strip
|
163
|
+
hash[key] = val
|
164
|
+
end
|
165
|
+
hash
|
166
|
+
end
|
167
|
+
|
168
|
+
# add list_of_xxx methods
|
169
|
+
#
|
170
|
+
List.dup.each do |type|
|
171
|
+
next if type.to_s =~ %r/list/
|
172
|
+
%W" list_of_#{ type } list_of_#{ type }s ".each do |m|
|
173
|
+
define_method m do |*objs|
|
174
|
+
list(*objs).map{|obj| send type, obj}
|
175
|
+
end
|
176
|
+
export m
|
177
|
+
List << m
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
# add list_of_xxx_from_file
|
182
|
+
#
|
183
|
+
List.dup.each do |type|
|
184
|
+
next if type.to_s =~ %r/list/
|
185
|
+
%W" list_of_#{ type }_from_file list_of_#{ type }s_from_file ".each do |m|
|
186
|
+
define_method m do |*args|
|
187
|
+
buf = nil
|
188
|
+
if args.size == 1 and args.first.respond_to?(:read)
|
189
|
+
buf = args.first.read
|
190
|
+
else
|
191
|
+
open(*args){|io| buf = io.read}
|
192
|
+
end
|
193
|
+
send(m.sub(/_from_file/, ''), buf)
|
194
|
+
end
|
195
|
+
export m
|
196
|
+
List << m
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
def self.[] sym
|
201
|
+
prefix = sym.to_s.downcase.to_sym
|
202
|
+
candidates = List.select{|m| m =~ %r/^#{ prefix }/i}
|
203
|
+
m = candidates.shift
|
204
|
+
raise ArgumentError, "unsupported coercion: #{ sym.inspect } (#{ List.join ',' })" unless
|
205
|
+
m
|
206
|
+
raise ArgumentError, "ambiguous coercion: #{ sym.inspect } (#{ List.join ',' })" unless
|
207
|
+
candidates.empty? or m.to_s == sym.to_s
|
208
|
+
lambda{|obj| method(m).call obj}
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|