dao 4.2.1 → 4.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. data/README +103 -63
  2. data/Rakefile +3 -3
  3. data/dao.gemspec +27 -16
  4. data/lib/dao.rb +17 -17
  5. data/lib/dao/active_record.rb +1 -0
  6. data/lib/dao/api.rb +2 -1
  7. data/lib/dao/api/{endpoints.rb → call.rb} +1 -0
  8. data/lib/dao/api/context.rb +2 -0
  9. data/lib/dao/api/dsl.rb +1 -0
  10. data/lib/dao/api/initializers.rb +1 -0
  11. data/lib/dao/api/modes.rb +1 -0
  12. data/lib/dao/api/routes.rb +1 -0
  13. data/lib/dao/blankslate.rb +1 -0
  14. data/lib/dao/conducer.rb +315 -274
  15. data/lib/dao/conducer/active_model.rb +98 -0
  16. data/lib/dao/conducer/attributes.rb +1 -0
  17. data/lib/dao/conducer/autocrud.rb +58 -0
  18. data/lib/dao/conducer/callback_support.rb +20 -0
  19. data/lib/dao/conducer/collection.rb +45 -0
  20. data/lib/dao/conducer/controller_support.rb +104 -0
  21. data/lib/dao/conducer/nav_support.rb +9 -0
  22. data/lib/dao/conducer/view_support.rb +16 -0
  23. data/lib/dao/data.rb +2 -1
  24. data/lib/dao/db.rb +2 -0
  25. data/lib/dao/endpoint.rb +1 -0
  26. data/lib/dao/engine.rb +1 -0
  27. data/lib/dao/errors.rb +109 -99
  28. data/lib/dao/exceptions.rb +1 -0
  29. data/lib/dao/extractor.rb +1 -0
  30. data/lib/dao/form.rb +175 -20
  31. data/lib/dao/instance_exec.rb +1 -0
  32. data/lib/dao/mode.rb +1 -0
  33. data/lib/dao/mongo_mapper.rb +1 -0
  34. data/lib/dao/name.rb +1 -0
  35. data/lib/dao/params.rb +2 -1
  36. data/lib/dao/path.rb +1 -0
  37. data/lib/dao/path_map.rb +24 -0
  38. data/lib/dao/rack.rb +1 -0
  39. data/lib/dao/rack/middleware.rb +1 -0
  40. data/lib/dao/rack/middleware/params_parser.rb +1 -0
  41. data/lib/dao/rails.rb +12 -32
  42. data/lib/dao/rails/lib/generators/dao/USAGE +2 -2
  43. data/lib/dao/rails/lib/generators/dao/dao_generator.rb +8 -27
  44. data/lib/dao/rails/lib/generators/dao/templates/api.rb +2 -1
  45. data/lib/dao/rails/lib/generators/dao/templates/api_controller.rb +22 -20
  46. data/lib/dao/rails/lib/generators/dao/templates/conducer.rb +49 -43
  47. data/lib/dao/rails/lib/generators/dao/templates/dao.css +26 -25
  48. data/lib/dao/rails/lib/generators/dao/templates/dao.js +3 -0
  49. data/lib/dao/rails/lib/generators/dao/templates/dao_helper.rb +58 -45
  50. data/lib/dao/result.rb +50 -1
  51. data/lib/dao/route.rb +1 -0
  52. data/lib/dao/slug.rb +12 -36
  53. data/lib/dao/status.rb +91 -7
  54. data/lib/dao/stdext.rb +1 -0
  55. data/lib/dao/support.rb +90 -80
  56. data/lib/dao/upload.rb +396 -0
  57. data/lib/dao/validations.rb +23 -5
  58. data/lib/dao/validations/callback.rb +5 -0
  59. data/lib/dao/validations/common.rb +100 -3
  60. data/lib/dao/validations/instance.rb +17 -0
  61. data/lib/dao/validations/validator.rb +192 -91
  62. data/test/active_model_conducer_lint_test.rb +1 -0
  63. data/test/api_test.rb +15 -0
  64. data/test/conducer_test.rb +608 -90
  65. data/test/data/han-solo.jpg +0 -0
  66. data/test/form_test.rb +1 -0
  67. data/test/helper.rb +1 -0
  68. data/test/leak.rb +1 -0
  69. data/test/support_test.rb +4 -1
  70. data/test/testing.rb +1 -0
  71. data/test/validations_test.rb +176 -30
  72. metadata +120 -131
  73. data/b.rb +0 -38
  74. data/lib/dao/conducer/crud.rb +0 -70
  75. data/lib/dao/current.rb +0 -66
  76. data/lib/dao/image_cache.rb +0 -193
  77. data/lib/dao/rails/lib/generators/dao/templates/conducer_controller.rb +0 -79
  78. data/test/db.yml +0 -9
data/b.rb DELETED
@@ -1,38 +0,0 @@
1
-
2
- require 'dao'
3
-
4
-
5
-
6
- class C < ::Map
7
- include Dao::Validations
8
- validates_presence_of :foo
9
- end
10
-
11
- c = C.new
12
-
13
- p c.valid?
14
- p c.errors
15
-
16
- c.update(:foo => 42)
17
-
18
- p c.valid?
19
- p c.errors
20
-
21
- #p 'c.validator.attributes' => c.validator.attributes
22
- #p 'c' => c
23
-
24
- __END__
25
-
26
- p c.valid?
27
-
28
-
29
- p c
30
-
31
- p c.valid?
32
-
33
- #c.validator
34
- #p c.validator.object
35
- #p c.valid?
36
- #p c.errors
37
- #p c.status
38
-
@@ -1,70 +0,0 @@
1
- module Dao
2
- ## CRUD support
3
- #
4
- class Conducer
5
- module CRUD
6
- Code = proc do
7
- class << self
8
- def db
9
- Db.instance
10
- end
11
-
12
- def collection
13
- db[collection_name]
14
- end
15
-
16
- def all(*args)
17
- hashes = collection.all()
18
- hashes.map{|hash| new(hash)}
19
- end
20
-
21
- def find(id)
22
- hash = collection.find(id)
23
- new(hash) if hash
24
- end
25
- end
26
-
27
- def save
28
- run_callbacks :save do
29
- return(false) unless valid?
30
- id = self.class.collection.save(@attributes)
31
- @attributes.set(:id => id)
32
- true
33
- end
34
- ensure
35
- @new_record = false
36
- end
37
-
38
- def destroy
39
- id = self.id
40
- if id
41
- self.class.collection.destroy(id)
42
- @attributes.rm(:id)
43
- end
44
- id
45
- ensure
46
- @destroyed = true
47
- end
48
- end
49
-
50
- def CRUD.included(other)
51
- super
52
- ensure
53
- other.module_eval(&Code)
54
- end
55
- end
56
- end
57
-
58
- ## dsl for auto-crud
59
- #
60
- class Conducer
61
- class << self
62
- def crud
63
- include(Conducer::CRUD)
64
- end
65
- alias_method('crud!', 'crud')
66
- alias_method('autocrud!', 'crud')
67
- end
68
- end
69
- #Conducer::send(:include, Conducer::CRUD)
70
- end
data/lib/dao/current.rb DELETED
@@ -1,66 +0,0 @@
1
- # FIXME - the redundancy in this file fucking kilz me... fix it.
2
- #
3
- module Dao::Current
4
- Methods = proc do
5
- end
6
-
7
- ClassMethods = proc do
8
- def current_controller
9
- @current_controller ||= (
10
- if defined?(@controller)
11
- @controller
12
- else
13
- Dao.current_controller || Dao.mock_controller
14
- end
15
- )
16
- end
17
-
18
- %w( request response session ).each do |attr|
19
- instance_eval <<-__, __FILE__, __LINE__
20
- def current_#{ attr }
21
- @current_#{ attr } ||= current_controller.instance_eval{ #{ attr } }
22
- end
23
- __
24
- end
25
-
26
- %w( current_user effective_user real_user ).each do |attr|
27
- instance_eval <<-__, __FILE__, __LINE__
28
- def #{ attr }
29
- @#{ attr } ||= current_controller.instance_eval{ #{ attr } }
30
- end
31
- def #{ attr }=(value)
32
- @#{ attr } = value
33
- end
34
- __
35
- end
36
- end
37
-
38
- InstanceMethods = proc do
39
- def current_controller
40
- @current_controller ||= (
41
- if defined?(@controller)
42
- @controller
43
- else
44
- self.class.current_controller
45
- end
46
- )
47
- end
48
-
49
- %w( current_request current_response current_session current_user effective_user real_user ).each do |attr|
50
- module_eval <<-__, __FILE__, __LINE__
51
- def #{ attr }
52
- @#{ attr } ||= self.class.send('#{ attr }')
53
- end
54
- def #{ attr }=(value)
55
- @#{ attr } = value
56
- end
57
- __
58
- end
59
- end
60
-
61
- def self.included(other)
62
- other.send(:instance_eval, &ClassMethods)
63
- other.send(:class_eval, &InstanceMethods)
64
- super
65
- end
66
- end
@@ -1,193 +0,0 @@
1
- module Dao
2
- require 'uuidtools'
3
- require 'fileutils'
4
- require 'cgi'
5
-
6
- class ImageCache
7
- Version = '1.0.0'
8
-
9
- class << ImageCache
10
- def version
11
- ImageCache::Version
12
- end
13
-
14
- def base
15
- @base ||= 'images/cache'
16
- end
17
-
18
- def base=(base)
19
- @base = base.to_s.sub(%r|^/+|, '')
20
- end
21
-
22
- def url
23
- '/' + base
24
- end
25
-
26
- def root
27
- @root ||= File.join(Rails.root, 'public', base)
28
- end
29
-
30
- def uuid(*args)
31
- UUIDTools::UUID.timestamp_create.to_s
32
- end
33
-
34
- def tmpdir(&block)
35
- tmpdir = File.join(root, uuid)
36
-
37
- if block
38
- FileUtils.mkdir_p(tmpdir)
39
- block.call(tmpdir)
40
- else
41
- tmpdir
42
- end
43
- end
44
-
45
- def cleanname(path)
46
- basename = File.basename(path.to_s)
47
- CGI.unescape(basename).gsub(%r/[^0-9a-zA-Z_@)(~.-]/, '_').gsub(%r/_+/,'_')
48
- end
49
-
50
- def cache_key_for(key)
51
- "#{ key }__cache"
52
- end
53
-
54
- def for(params, key = :image)
55
- image = params[key]
56
- if image.respond_to?(:read)
57
- tmpdir do |tmp|
58
- basename = cleanname(image.original_path)
59
-
60
- path = File.join(tmp, basename)
61
- open(path, 'w'){|fd| fd.write(image.read)}
62
- image_cache = new(key, path)
63
- params[key] = image_cache.io
64
- return image_cache
65
- end
66
- end
67
-
68
- cache_key = cache_key_for(key)
69
- image_cache = params[cache_key]
70
- if image_cache
71
- dirname, basename = File.split(image_cache)
72
- path = root + '/' + File.join(File.basename(dirname), basename)
73
- image_cache = new(key, path)
74
- params[key] = image_cache.io
75
- return image_cache
76
- end
77
-
78
- return new(key, path=nil)
79
- end
80
-
81
- def finalizer(object_id)
82
- if fd = IOs[object_id]
83
- IO.for_fd(fd).close
84
- IOs.delete(object_id)
85
- end
86
- end
87
-
88
- UUIDPattern = %r/^[a-zA-Z0-9-]+$/io
89
- Age = 60 * 60 * 24
90
-
91
- def clear!(options = {})
92
- glob = File.join(root, '*')
93
- age = Integer(options[:age] || options['age'] || Age)
94
- since = options[:since] || options['since'] || Time.now
95
-
96
- Dir.glob(glob) do |entry|
97
- begin
98
- next unless test(?d, entry)
99
- next unless File.basename(entry) =~ UUIDPattern
100
-
101
- files = Dir.glob(File.join(entry, '**/**'))
102
-
103
- all_files_are_old =
104
- files.all? do |file|
105
- begin
106
- stat = File.stat(file)
107
- age = since - stat.atime
108
- age >= Age
109
- rescue
110
- false
111
- end
112
- end
113
-
114
- FileUtils.rm_rf(entry) if all_files_are_old
115
- rescue
116
- next
117
- end
118
- end
119
- end
120
- end
121
-
122
- attr_accessor :key
123
- attr_accessor :cache_key
124
- attr_accessor :path
125
- attr_accessor :dirname
126
- attr_accessor :basename
127
- attr_accessor :value
128
- attr_accessor :io
129
-
130
- IOs = {}
131
-
132
- def initialize(key, path)
133
- @key = key.to_s
134
- @cache_key = ImageCache.cache_key_for(key)
135
-
136
- if path
137
- @path = path
138
- @dirname, @basename = File.split(@path)
139
- @value = File.join(File.basename(@dirname), @basename).strip
140
- @io = open(@path)
141
- IOs[object_id] = @io.fileno
142
- ObjectSpace.define_finalizer(self, ImageCache.method(:finalizer).to_proc)
143
- else
144
- @path = nil
145
- @value = nil
146
- end
147
- end
148
-
149
- def hidden
150
- raw("<input type='hidden' name='#{ @cache_key }' value='#{ @value }' />") if @value
151
- end
152
-
153
- def to_s
154
- hidden.to_s
155
- end
156
-
157
- def url
158
- File.join(ImageCache.url, @value) if @value
159
- end
160
-
161
- def raw(*args)
162
- string = args.join
163
- if string.respond_to?(:html_safe)
164
- string.html_safe
165
- else
166
- string
167
- end
168
- end
169
-
170
- def clear!
171
- FileUtils.rm_rf(@dirname) if test(?d, @dirname)
172
- rescue
173
- nil
174
- ensure
175
- if @io
176
- @io.close
177
- IOs.delete(object_id)
178
- end
179
- Thread.new{ ImageCache.clear! }
180
- end
181
- end
182
-
183
- Image_cache = ImageCache unless defined?(Image_cache)
184
-
185
- if defined?(Rails.env)
186
- unless Rails.env.production?
187
- if defined?(unloadable)
188
- unloadable(ImageCache)
189
- unloadable(Image_cache)
190
- end
191
- end
192
- end
193
- end
@@ -1,79 +0,0 @@
1
- <%
2
-
3
- conducer_class_name = @conducer_name.camelize
4
- controller_class_name = @controller_name.camelize
5
-
6
- -%>
7
- class PostsController < ApplicationController
8
- Post = PostConducer
9
-
10
- def index
11
- @posts = Post.for_index(params)
12
-
13
- respond_to do |format|
14
- format.html # index.html.erb
15
- format.json { render :json => @posts }
16
- end
17
- end
18
-
19
- def show
20
- @post = Post.for_show(params[:id])
21
-
22
- respond_to do |format|
23
- format.html # show.html.erb
24
- format.json { render :json => @post }
25
- end
26
- end
27
-
28
- def new
29
- @post = Post.for_new(params)
30
-
31
- respond_to do |format|
32
- format.html # new.html.erb
33
- format.json { render :json => @post }
34
- end
35
- end
36
-
37
- def edit
38
- @post = Post.find(params[:id])
39
- end
40
-
41
- def create
42
- @post = Post.new(params[:post])
43
-
44
- respond_to do |format|
45
- if @post.save
46
- format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
47
- format.json { render :json => @post, :status => :created, :location => @post }
48
- else
49
- format.html { render :action => "new" }
50
- format.json { render :json => @post.errors, :status => :unprocessable_entity }
51
- end
52
- end
53
- end
54
-
55
- def update
56
- @post = Post.find(params[:id])
57
-
58
- respond_to do |format|
59
- if @post.update_attributes(params[:post])
60
- format.html { redirect_to(@post, :notice => 'Post was successfully updated.') }
61
- format.json { head :ok }
62
- else
63
- format.html { render :action => "edit" }
64
- format.json { render :json => @post.errors, :status => :unprocessable_entity }
65
- end
66
- end
67
- end
68
-
69
- def destroy
70
- @post = Post.find(params[:id])
71
- @post.destroy
72
-
73
- respond_to do |format|
74
- format.html { redirect_to(posts_url) }
75
- format.json { head :ok }
76
- end
77
- end
78
- end
79
-