dao 5.5.0 → 8.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +6 -14
  2. data/README.md +258 -0
  3. data/Rakefile +4 -5
  4. data/coerce-0.0.8/README +28 -0
  5. data/coerce-0.0.8/Rakefile +392 -0
  6. data/coerce-0.0.8/coerce.gemspec +31 -0
  7. data/coerce-0.0.8/lib/coerce.rb +210 -0
  8. data/dao.gemspec +38 -25
  9. data/lib/dao.rb +18 -81
  10. data/lib/dao/_lib.rb +42 -0
  11. data/lib/dao/active_record.rb +8 -8
  12. data/lib/dao/api/call.rb +19 -4
  13. data/lib/dao/api/dsl.rb +1 -1
  14. data/lib/dao/coerce.rb +211 -0
  15. data/lib/dao/conducer.rb +10 -14
  16. data/lib/dao/conducer/controller_support.rb +5 -0
  17. data/lib/dao/conducer/view_support.rb +0 -2
  18. data/lib/dao/db.rb +0 -1
  19. data/lib/dao/errors.rb +17 -11
  20. data/lib/dao/errors2html.rb +128 -0
  21. data/lib/dao/form.rb +13 -16
  22. data/lib/dao/messages.rb +0 -4
  23. data/lib/dao/path.rb +1 -1
  24. data/lib/dao/route.rb +2 -2
  25. data/lib/dao/status.rb +3 -4
  26. data/lib/dao/support.rb +26 -19
  27. data/lib/dao/upload.rb +0 -1
  28. data/lib/dao/validations/common.rb +6 -6
  29. data/lib/dao/validations/validator.rb +3 -3
  30. data/lib/dao/wrap.rb +259 -0
  31. data/tasks/default.rake +207 -0
  32. data/tasks/this.rb +207 -0
  33. data/test/active_model_conducer_lint_test.rb +3 -11
  34. data/test/api_test.rb +24 -35
  35. data/test/conducer_test.rb +37 -47
  36. data/test/errors_test.rb +29 -13
  37. data/test/form_test.rb +24 -34
  38. data/test/rake_rerun_reporter.rb +74 -0
  39. data/test/support_test.rb +9 -14
  40. data/test/test_helper.rb +220 -0
  41. data/test/{helper.rb → util.rb} +0 -0
  42. data/test/validations_test.rb +14 -28
  43. data/wrap-1.5.2/README +57 -0
  44. data/wrap-1.5.2/Rakefile +394 -0
  45. data/wrap-1.5.2/lib/wrap.rb +295 -0
  46. data/{test → wrap-1.5.2/test}/testing.rb +0 -1
  47. data/wrap-1.5.2/test/wrap_test.rb +397 -0
  48. data/wrap-1.5.2/wrap.gemspec +38 -0
  49. metadata +47 -103
  50. data/Gemfile +0 -16
  51. data/Gemfile.lock +0 -118
  52. data/README +0 -256
@@ -0,0 +1,42 @@
1
+ module Dao
2
+ Version = '8.0.0' unless defined?(Version)
3
+
4
+ def version
5
+ Dao::Version
6
+ end
7
+
8
+ def dependencies
9
+ {
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' ] ,
15
+ }
16
+ end
17
+
18
+ def description
19
+ "presenter, conductor, api, and better form objects for you rails' pleasure"
20
+ end
21
+
22
+ def libdir(*args, &block)
23
+ @libdir ||= File.dirname(File.expand_path(__FILE__).sub(/\.rb$/,''))
24
+ args.empty? ? @libdir : File.join(@libdir, *args)
25
+ ensure
26
+ if block
27
+ begin
28
+ $LOAD_PATH.unshift(@libdir)
29
+ block.call()
30
+ ensure
31
+ $LOAD_PATH.shift()
32
+ end
33
+ end
34
+ end
35
+
36
+ def load(*libs)
37
+ libs = libs.join(' ').scan(/[^\s+]+/)
38
+ Dao.libdir{ libs.each{|lib| Kernel.load(lib) } }
39
+ end
40
+
41
+ extend Dao
42
+ end
@@ -1,7 +1,7 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  begin
3
- ActiveRecord
4
- ActiveRecord::Base
3
+ ActiveRecord.name
4
+ ActiveRecord::Base.name
5
5
  rescue NameError
6
6
  nil
7
7
  end
@@ -12,10 +12,10 @@ if defined?(ActiveRecord)
12
12
  class Base
13
13
  def Base.to_dao(*args)
14
14
  if args.first.is_a?(Base)
15
- record_to_dao(record = args.shift, *args)
15
+ record_to_dao(args.shift, *args)
16
16
  else
17
17
  @to_dao ||= (
18
- names = column_names ### + reflect_on_all_associations.map(&:name)
18
+ column_names ### + reflect_on_all_associations.map(&:name)
19
19
  )
20
20
  @to_dao = Array(args) unless args.empty?
21
21
  @to_dao
@@ -101,10 +101,10 @@ if defined?(ActiveRecord)
101
101
  end
102
102
 
103
103
  if attr.is_a?(Hash)
104
- attr.each do |related, argv|
105
- v = record.send(related)
106
- value = v.respond_to?(:to_dao) ? v.to_dao(*argv) : v
107
- map[related] = value
104
+ attr.each do |rel, _argv|
105
+ v = record.send(rel)
106
+ value = v.respond_to?(:to_dao) ? v.to_dao(*_argv) : v
107
+ map[rel] = value
108
108
  end
109
109
  next
110
110
  end
@@ -13,13 +13,14 @@ module Dao
13
13
  :endpoints => Map.new,
14
14
  :blocks => {},
15
15
  :README => [],
16
- :docs => []
16
+ :docs => [],
17
+ :paths => [],
17
18
  }
18
19
  end
19
20
 
20
21
  def call(*args, &block)
21
22
  options = Dao.options_for!(args)
22
- path = Path.new(args.shift || raise(ArgumentError, "no path!"))
23
+ path = Path.new(args.shift || paths.shift || raise(ArgumentError, "no path!"))
23
24
 
24
25
  api = self
25
26
 
@@ -44,7 +45,12 @@ module Dao
44
45
 
45
46
  endpoints[path] = endpoint
46
47
  end
47
- alias_method('endpoint', 'call')
48
+
49
+ def endpoint(path, &block)
50
+ paths << path.to_s
51
+ class_eval(&block)
52
+ end
53
+ #alias_method('endpoint', 'call')
48
54
 
49
55
  def endpoints
50
56
  state[:endpoints]
@@ -71,6 +77,10 @@ module Dao
71
77
  state[:docs]
72
78
  end
73
79
 
80
+ def paths
81
+ state[:paths]
82
+ end
83
+
74
84
  def readme(*args)
75
85
  if args.empty?
76
86
  state[:README]
@@ -232,7 +242,12 @@ module Dao
232
242
 
233
243
  # delgate some methods to the context
234
244
  #
235
- Context.attrs.each do |method|
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
+
236
251
  module_eval <<-__, __FILE__, __LINE__
237
252
  def #{ method }(*args)
238
253
  context.send(#{ method.inspect }, *args)
@@ -33,7 +33,7 @@ module Dao
33
33
 
34
34
  class << Api
35
35
  def evaluate(&block)
36
- @dsl ||= DSL.new(api=self)
36
+ @dsl ||= DSL.new(self)
37
37
  @dsl.evaluate(&block)
38
38
  end
39
39
  alias_method('configure', 'evaluate')
@@ -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
@@ -86,14 +86,10 @@ module Dao
86
86
  # instance methods
87
87
  #
88
88
  %w[
89
- attributes
90
- form
91
89
  params
92
- errors
93
90
  messages
94
91
  models
95
92
  model
96
- conduces
97
93
  ].each{|attr| fattr(attr)}
98
94
 
99
95
  # ctors
@@ -117,7 +113,7 @@ module Dao
117
113
  controller.send(:action_name).to_s
118
114
  end
119
115
 
120
- conducer = new(Action.new(action), *args, &block)
116
+ new(Action.new(action), *args, &block)
121
117
  end
122
118
 
123
119
  def Conducer.call(*args, &block)
@@ -243,7 +239,7 @@ module Dao
243
239
  end
244
240
 
245
241
  def conduces?(model)
246
- if @model
242
+ if defined?(@model)
247
243
  @model == model
248
244
  else
249
245
  self.class.conduces?(model)
@@ -323,6 +319,10 @@ module Dao
323
319
  update_attributes(attributes)
324
320
  end
325
321
 
322
+ def attributes
323
+ @attributes
324
+ end
325
+
326
326
  def set(*args, &block)
327
327
  update_attributes(*args, &block)
328
328
  end
@@ -348,8 +348,8 @@ module Dao
348
348
  def method_missing(method, *args, &block)
349
349
  re = /^([^=!?]+)([=!?])?$/imox
350
350
 
351
- matched, key, suffix = re.match(method.to_s).to_a
352
-
351
+ _, key, suffix = re.match(method.to_s).to_a
352
+
353
353
  case suffix
354
354
  when '='
355
355
  set(key, args.first)
@@ -399,10 +399,10 @@ module Dao
399
399
  end
400
400
 
401
401
  def id_for(object)
402
- model?(object) ? object.id : object
402
+ modelish?(object) ? object.id : object
403
403
  end
404
404
 
405
- def model?(object)
405
+ def modelish?(object)
406
406
  object.respond_to?(:persisted?)
407
407
  end
408
408
 
@@ -478,10 +478,6 @@ module Dao
478
478
  Dao.key_for(key)
479
479
  end
480
480
 
481
- def errors
482
- validator.errors
483
- end
484
-
485
481
  def model_name
486
482
  self.class.model_name
487
483
  end
@@ -28,6 +28,11 @@ module Dao
28
28
  @controller.send(:request) if @controller
29
29
  end
30
30
 
31
+ def request_method
32
+ method = request.try(:method)
33
+ method ? method.to_s.to_sym.upcase : nil
34
+ end
35
+
31
36
  ##
32
37
  #
33
38
  class Action < ::String
@@ -10,9 +10,7 @@ module Dao
10
10
  def install_routes!
11
11
  url_helpers = Rails.application.try(:routes).try(:url_helpers)
12
12
  include(url_helpers) if url_helpers
13
- include(ActionView::Helpers) if defined?(ActionView::Helpers)
14
13
  extend(url_helpers) if url_helpers
15
- extend(ActionView::Helpers) if defined?(ActionView::Helpers)
16
14
  end
17
15
  end
18
16
  end