ambition 0.3.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. data/Manifest +41 -48
  2. data/README +6 -203
  3. data/ambition.gemspec +111 -0
  4. data/app_generators/ambition_adapter/USAGE +1 -0
  5. data/app_generators/ambition_adapter/ambition_adapter_generator.rb +66 -0
  6. data/app_generators/ambition_adapter/templates/LICENSE +18 -0
  7. data/app_generators/ambition_adapter/templates/README +6 -0
  8. data/app_generators/ambition_adapter/templates/Rakefile +31 -0
  9. data/app_generators/ambition_adapter/templates/lib/base.rb.erb +12 -0
  10. data/app_generators/ambition_adapter/templates/lib/init.rb.erb +22 -0
  11. data/app_generators/ambition_adapter/templates/lib/query.rb.erb +52 -0
  12. data/app_generators/ambition_adapter/templates/lib/select.rb.erb +100 -0
  13. data/app_generators/ambition_adapter/templates/lib/slice.rb.erb +19 -0
  14. data/app_generators/ambition_adapter/templates/lib/sort.rb.erb +43 -0
  15. data/app_generators/ambition_adapter/templates/test/helper.rb.erb +9 -0
  16. data/app_generators/ambition_adapter/templates/test/select_test.rb.erb +157 -0
  17. data/app_generators/ambition_adapter/templates/test/slice_test.rb.erb +36 -0
  18. data/app_generators/ambition_adapter/templates/test/sort_test.rb.erb +53 -0
  19. data/bin/ambition_adapter +13 -0
  20. data/lib/ambition.rb +8 -13
  21. data/lib/ambition/api.rb +42 -35
  22. data/lib/ambition/context.rb +62 -0
  23. data/lib/ambition/{proc_to_ruby.rb → core_ext.rb} +13 -0
  24. data/lib/ambition/enumerable.rb +6 -0
  25. data/lib/ambition/processors/base.rb +126 -0
  26. data/lib/ambition/processors/ruby.rb +24 -0
  27. data/lib/ambition/processors/select.rb +105 -0
  28. data/lib/ambition/processors/slice.rb +15 -0
  29. data/lib/ambition/processors/sort.rb +51 -0
  30. data/test/adapters/exemplar/association_test.rb +34 -0
  31. data/test/adapters/exemplar/count_test.rb +0 -0
  32. data/test/adapters/exemplar/detect_test.rb +9 -0
  33. data/test/adapters/exemplar/enumerable_test.rb +0 -0
  34. data/test/adapters/exemplar/helper.rb +3 -0
  35. data/test/adapters/exemplar/index_operator.rb +6 -0
  36. data/test/adapters/exemplar/reject_test.rb +0 -0
  37. data/test/adapters/exemplar/select_test.rb +151 -0
  38. data/test/adapters/exemplar/slice_test.rb +0 -0
  39. data/test/adapters/exemplar/sort_test.rb +0 -0
  40. data/test/debug +9 -0
  41. data/test/helper.rb +2 -52
  42. metadata +56 -71
  43. data/Rakefile +0 -64
  44. data/init.rb +0 -1
  45. data/lib/ambition/database_statements.rb +0 -31
  46. data/lib/ambition/processor.rb +0 -123
  47. data/lib/ambition/query.rb +0 -91
  48. data/lib/ambition/ruby_processor.rb +0 -22
  49. data/lib/ambition/select_processor.rb +0 -149
  50. data/lib/ambition/simple_processor.rb +0 -10
  51. data/lib/ambition/sort_processor.rb +0 -47
  52. data/lib/ambition/source.rb +0 -53
  53. data/test/benchmark.rb +0 -68
  54. data/test/chaining_test.rb +0 -34
  55. data/test/console +0 -9
  56. data/test/count_test.rb +0 -17
  57. data/test/databases/boot.rb +0 -3
  58. data/test/databases/database.yml +0 -17
  59. data/test/databases/fixtures/admin.rb +0 -3
  60. data/test/databases/fixtures/companies.yml +0 -24
  61. data/test/databases/fixtures/company.rb +0 -23
  62. data/test/databases/fixtures/developer.rb +0 -11
  63. data/test/databases/fixtures/developers_projects.yml +0 -13
  64. data/test/databases/fixtures/project.rb +0 -4
  65. data/test/databases/fixtures/projects.yml +0 -7
  66. data/test/databases/fixtures/replies.yml +0 -20
  67. data/test/databases/fixtures/reply.rb +0 -5
  68. data/test/databases/fixtures/topic.rb +0 -19
  69. data/test/databases/fixtures/topics.yml +0 -32
  70. data/test/databases/fixtures/user.rb +0 -2
  71. data/test/databases/fixtures/users.yml +0 -35
  72. data/test/databases/lib/activerecord_test_connector.rb +0 -65
  73. data/test/databases/lib/load_fixtures.rb +0 -13
  74. data/test/databases/lib/schema.rb +0 -41
  75. data/test/enumerable_test.rb +0 -95
  76. data/test/join_test.rb +0 -61
  77. data/test/limit_test.rb +0 -41
  78. data/test/order_test.rb +0 -52
  79. data/test/profiler.rb +0 -34
  80. data/test/ruby_test.rb +0 -9
  81. data/test/source_test.rb +0 -43
  82. data/test/types_test.rb +0 -59
  83. data/test/where_test.rb +0 -245
metadata CHANGED
@@ -1,26 +1,26 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.0
2
+ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: ambition
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.3.1
7
- date: 2007-11-06 00:00:00 -08:00
8
- summary: Ambition builds SQL from plain jane Ruby.
6
+ version: 0.5.0
7
+ date: 2008-01-29 00:00:00 -08:00
8
+ summary: Ambition builds yer API calls from plain jane Ruby.
9
9
  require_paths:
10
10
  - lib
11
11
  email: chris@ozmm.org
12
12
  homepage: http://errtheblog.com/
13
13
  rubyforge_project: err
14
- description: Ambition builds SQL from plain jane Ruby.
14
+ description: Ambition builds yer API calls from plain jane Ruby.
15
15
  autorequire:
16
16
  default_executable:
17
17
  bindir: bin
18
18
  has_rdoc: true
19
19
  required_ruby_version: !ruby/object:Gem::Version::Requirement
20
20
  requirements:
21
- - - ">"
21
+ - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.0.0
23
+ version: 1.8.6
24
24
  version:
25
25
  platform: ruby
26
26
  signing_key:
@@ -29,71 +29,56 @@ post_install_message:
29
29
  authors:
30
30
  - Chris Wanstrath
31
31
  files:
32
- - ./init.rb
33
- - ./lib/ambition/api.rb
34
- - ./lib/ambition/database_statements.rb
35
- - ./lib/ambition/proc_to_ruby.rb
36
- - ./lib/ambition/processor.rb
37
- - ./lib/ambition/query.rb
38
- - ./lib/ambition/ruby_processor.rb
39
- - ./lib/ambition/select_processor.rb
40
- - ./lib/ambition/simple_processor.rb
41
- - ./lib/ambition/sort_processor.rb
42
- - ./lib/ambition/source.rb
43
- - ./lib/ambition.rb
44
- - ./LICENSE
45
- - ./Manifest
46
- - ./Rakefile
47
- - ./README
48
- - ./test/benchmark.rb
49
- - ./test/chaining_test.rb
50
- - ./test/console
51
- - ./test/count_test.rb
52
- - ./test/databases/boot.rb
53
- - ./test/databases/database.yml
54
- - ./test/databases/fixtures/admin.rb
55
- - ./test/databases/fixtures/companies.yml
56
- - ./test/databases/fixtures/company.rb
57
- - ./test/databases/fixtures/developer.rb
58
- - ./test/databases/fixtures/developers_projects.yml
59
- - ./test/databases/fixtures/project.rb
60
- - ./test/databases/fixtures/projects.yml
61
- - ./test/databases/fixtures/replies.yml
62
- - ./test/databases/fixtures/reply.rb
63
- - ./test/databases/fixtures/topic.rb
64
- - ./test/databases/fixtures/topics.yml
65
- - ./test/databases/fixtures/user.rb
66
- - ./test/databases/fixtures/users.yml
67
- - ./test/databases/lib/activerecord_test_connector.rb
68
- - ./test/databases/lib/load_fixtures.rb
69
- - ./test/databases/lib/schema.rb
70
- - ./test/enumerable_test.rb
71
- - ./test/helper.rb
72
- - ./test/join_test.rb
73
- - ./test/limit_test.rb
74
- - ./test/order_test.rb
75
- - ./test/profiler.rb
76
- - ./test/source_test.rb
77
- - ./test/ruby_test.rb
78
- - ./test/types_test.rb
79
- - ./test/where_test.rb
80
- test_files:
81
- - test/chaining_test.rb
82
- - test/count_test.rb
83
- - test/enumerable_test.rb
84
- - test/join_test.rb
85
- - test/limit_test.rb
86
- - test/order_test.rb
87
- - test/ruby_test.rb
88
- - test/source_test.rb
89
- - test/types_test.rb
90
- - test/where_test.rb
32
+ - app_generators/ambition_adapter/ambition_adapter_generator.rb
33
+ - app_generators/ambition_adapter/templates/lib/base.rb.erb
34
+ - app_generators/ambition_adapter/templates/lib/init.rb.erb
35
+ - app_generators/ambition_adapter/templates/lib/query.rb.erb
36
+ - app_generators/ambition_adapter/templates/lib/select.rb.erb
37
+ - app_generators/ambition_adapter/templates/lib/slice.rb.erb
38
+ - app_generators/ambition_adapter/templates/lib/sort.rb.erb
39
+ - app_generators/ambition_adapter/templates/LICENSE
40
+ - app_generators/ambition_adapter/templates/Rakefile
41
+ - app_generators/ambition_adapter/templates/README
42
+ - app_generators/ambition_adapter/templates/test/helper.rb.erb
43
+ - app_generators/ambition_adapter/templates/test/select_test.rb.erb
44
+ - app_generators/ambition_adapter/templates/test/slice_test.rb.erb
45
+ - app_generators/ambition_adapter/templates/test/sort_test.rb.erb
46
+ - app_generators/ambition_adapter/USAGE
47
+ - bin/ambition_adapter
48
+ - lib/ambition/api.rb
49
+ - lib/ambition/context.rb
50
+ - lib/ambition/core_ext.rb
51
+ - lib/ambition/enumerable.rb
52
+ - lib/ambition/processors/base.rb
53
+ - lib/ambition/processors/ruby.rb
54
+ - lib/ambition/processors/select.rb
55
+ - lib/ambition/processors/slice.rb
56
+ - lib/ambition/processors/sort.rb
57
+ - lib/ambition.rb
58
+ - LICENSE
59
+ - Manifest
60
+ - README
61
+ - test/adapters/exemplar/association_test.rb
62
+ - test/adapters/exemplar/count_test.rb
63
+ - test/adapters/exemplar/detect_test.rb
64
+ - test/adapters/exemplar/enumerable_test.rb
65
+ - test/adapters/exemplar/helper.rb
66
+ - test/adapters/exemplar/index_operator.rb
67
+ - test/adapters/exemplar/reject_test.rb
68
+ - test/adapters/exemplar/select_test.rb
69
+ - test/adapters/exemplar/slice_test.rb
70
+ - test/adapters/exemplar/sort_test.rb
71
+ - test/debug
72
+ - test/helper.rb
73
+ - ambition.gemspec
74
+ test_files: []
75
+
91
76
  rdoc_options: []
92
77
 
93
78
  extra_rdoc_files: []
94
79
 
95
- executables: []
96
-
80
+ executables:
81
+ - ambition_adapter
97
82
  extensions: []
98
83
 
99
84
  requirements: []
@@ -118,11 +103,11 @@ dependencies:
118
103
  version: 1.1.7
119
104
  version:
120
105
  - !ruby/object:Gem::Dependency
121
- name: activerecord
106
+ name: rubigen
122
107
  version_requirement:
123
108
  version_requirements: !ruby/object:Gem::Version::Requirement
124
109
  requirements:
125
- - - ">="
110
+ - - "="
126
111
  - !ruby/object:Gem::Version
127
- version: 1.15.0
112
+ version: 1.1.1
128
113
  version:
data/Rakefile DELETED
@@ -1,64 +0,0 @@
1
- require 'rake'
2
- require 'rake/testtask'
3
- require 'rake/rdoctask'
4
-
5
- Version = '0.3.1'
6
-
7
- module Rake::TaskManager
8
- def delete_task(task_class, args, &block)
9
- task_name, deps = resolve_args(args)
10
- @tasks.delete(task_class.scope_name(@scope, task_name).to_s)
11
- end
12
- end
13
- class Rake::Task
14
- def self.delete_task(args, &block) Rake.application.delete_task(self, args, &block) end
15
- end
16
- def delete_task(args, &block) Rake::Task.delete_task(args, &block) end
17
-
18
- begin
19
- require 'rubygems'
20
- gem 'echoe', '=1.3'
21
- ENV['RUBY_FLAGS'] = ""
22
- require 'echoe'
23
-
24
- Echoe.new('ambition', Version) do |p|
25
- p.rubyforge_name = 'err'
26
- p.summary = "Ambition builds SQL from plain jane Ruby."
27
- p.description = "Ambition builds SQL from plain jane Ruby."
28
- p.url = "http://errtheblog.com/"
29
- p.author = 'Chris Wanstrath'
30
- p.email = "chris@ozmm.org"
31
- p.extra_deps << ['ParseTree', '=2.0.1']
32
- p.extra_deps << ['ruby2ruby', '=1.1.7']
33
- p.extra_deps << ['activerecord', '>=1.15.0']
34
- p.test_globs = 'test/*_test.rb'
35
- end
36
-
37
- rescue LoadError
38
- end
39
-
40
- delete_task :test
41
-
42
- Rake::TestTask.new('test') do |t|
43
- t.pattern = 'test/*_test.rb'
44
- end
45
-
46
- desc 'Default: run unit tests.'
47
- task :default => :test
48
-
49
- desc 'Generate RDoc documentation'
50
- Rake::RDocTask.new(:rdoc) do |rdoc|
51
- files = ['README', 'LICENSE', 'lib/**/*.rb']
52
- rdoc.rdoc_files.add(files)
53
- rdoc.main = "README" # page to start on
54
- rdoc.title = "ambition"
55
- rdoc.template = File.exists?(t="/Users/chris/ruby/projects/err/rock/template.rb") ? t : "/var/www/rock/template.rb"
56
- rdoc.rdoc_dir = 'doc' # rdoc output folder
57
- rdoc.options << '--inline-source'
58
- end
59
-
60
- desc 'Generate coverage reports'
61
- task :rcov do
62
- `rcov -e gems test/*_test.rb`
63
- puts 'Generated coverage reports.'
64
- end
data/init.rb DELETED
@@ -1 +0,0 @@
1
- require 'ambition'
@@ -1,31 +0,0 @@
1
- module Ambition
2
- module DatabaseStatements
3
- def self.const_missing(*args)
4
- Abstract
5
- end
6
-
7
- class Abstract
8
- def regexp(options)
9
- 'REGEXP'
10
- end
11
- end
12
-
13
- class PostgreSQL < Abstract
14
- def regexp(regexp)
15
- if regexp.options == 1
16
- '~*'
17
- else
18
- '~'
19
- end
20
- end
21
-
22
- def negated_regexp(regexp)
23
- if regexp.options == 1
24
- '!~*'
25
- else
26
- '!~'
27
- end
28
- end
29
- end
30
- end
31
- end
@@ -1,123 +0,0 @@
1
- require 'active_record/connection_adapters/abstract/quoting'
2
- require 'ambition/proc_to_ruby'
3
-
4
- module Ambition
5
- class Processor
6
- include ActiveRecord::ConnectionAdapters::Quoting
7
-
8
- attr_reader :key, :join_string, :prefix, :includes
9
-
10
- def initialize
11
- @unsupported = [:alloca, :cfunc, :cref, :evstr, :ifunc, :last, :memo, :newline, :opt_n, :method] # internal nodes that you can't get to
12
- @includes = []
13
- end
14
-
15
- ##
16
- # Processing methods
17
- def process_proc(exp)
18
- receiver, body = process(exp.shift), exp.shift
19
- process(body)
20
- end
21
-
22
- def process_dasgn_curr(exp)
23
- @receiver = exp.first
24
- @receiver.to_s
25
- end
26
- alias_method :process_dasgn, :process_dasgn_curr
27
-
28
- def process_array(exp)
29
- # Branch on whether this is straight Ruby or a real array
30
- if ruby = rubify(exp)
31
- value ruby
32
- else
33
- arrayed = exp.map { |m| process(m) }
34
- arrayed.join(', ')
35
- end
36
- end
37
-
38
- def rubify(exp)
39
- if exp.first.first == :call && exp.first[1].last != @receiver && Array(exp.first[1][1]).last != @receiver
40
- RubyProcessor.process(exp.first)
41
- end
42
- end
43
-
44
- ##
45
- # Helper methods
46
- def to_s
47
- process(@block.to_sexp).squeeze(' ')
48
- end
49
-
50
- def value(variable)
51
- sanitize eval(variable, @block)
52
- end
53
-
54
- def sanitize(value)
55
- if value.is_a? Array
56
- return value.map { |v| sanitize(v) }.join(', ')
57
- end
58
-
59
- case value
60
- when true, 'true'
61
- '1'
62
- when false, 'false'
63
- '0'
64
- when Regexp
65
- "'#{value.source}'"
66
- else
67
- if active_connection?
68
- ActiveRecord::Base.connection.quote(value)
69
- else
70
- quote(value)
71
- end
72
- end
73
- rescue
74
- "'#{value}'"
75
- end
76
-
77
- def quote_column_name(value)
78
- if active_connection?
79
- ActiveRecord::Base.connection.quote_column_name(value)
80
- else
81
- value.to_s
82
- end
83
- end
84
-
85
- def active_connection?
86
- ActiveRecord::Base.active_connection_name
87
- end
88
-
89
- def statement(*args)
90
- @statement_instnace ||= DatabaseStatements.const_get(adapter_name).new
91
- @statement_instnace.send(*args)
92
- end
93
-
94
- def adapter_name
95
- ActiveRecord::Base.connection.adapter_name
96
- rescue ActiveRecord::ConnectionNotEstablished
97
- 'Abstract'
98
- end
99
-
100
- def extract_includes(receiver, method)
101
- return unless receiver.first == :call && receiver[1].last == @receiver
102
-
103
- if reflection = @owner.reflections[receiver.last]
104
- @includes << reflection.name unless @includes.include? reflection.name
105
- "#{reflection.table_name}.#{method}"
106
- else
107
- raise "No reflection `#{receiver.last}' found on #{@owner}"
108
- end
109
- end
110
-
111
- def process(node)
112
- node ||= []
113
-
114
- if respond_to?(method = "process_#{node.first}")
115
- send(method, node[1..-1])
116
- elsif node.blank?
117
- ''
118
- else
119
- raise "Missing process method for sexp: #{node.inspect}"
120
- end
121
- end
122
- end
123
- end
@@ -1,91 +0,0 @@
1
- module Ambition
2
- class Query
3
- @@select = 'SELECT * FROM %s %s'
4
-
5
- def initialize(owner)
6
- @table_name = owner.table_name
7
- @owner = owner
8
- @clauses = []
9
- end
10
-
11
- def add(clause)
12
- @clauses << clause
13
- self
14
- end
15
-
16
- def method_missing(method, *args, &block)
17
- with_context do
18
- @owner.send(method, *args, &block)
19
- end
20
- end
21
-
22
- def with_context
23
- @owner.query_context = self
24
- ret = yield
25
- ensure
26
- @owner.query_context = nil
27
- ret
28
- end
29
-
30
- def to_hash
31
- keyed = keyed_clauses
32
- hash = {}
33
-
34
- unless (where = keyed[:conditions]).blank?
35
- hash[:conditions] = Array(where)
36
- hash[:conditions] *= ' AND '
37
- end
38
-
39
- unless (includes = keyed[:includes]).blank?
40
- hash[:include] = includes.flatten
41
- end
42
-
43
- if order = keyed[:order]
44
- hash[:order] = order.join(', ')
45
- end
46
-
47
- if limit = keyed[:limit]
48
- hash[:limit] = limit.last
49
- end
50
-
51
- if offset = keyed[:offset]
52
- hash[:offset] = offset.last
53
- end
54
-
55
- hash
56
- end
57
-
58
- def to_s
59
- hash = keyed_clauses
60
-
61
- raise "Sorry, I can't construct SQL with complex joins (yet)" unless hash[:includes].blank?
62
-
63
- sql = []
64
- sql << "WHERE #{hash[:conditions].join(' AND ')}" unless hash[:conditions].blank?
65
- sql << "ORDER BY #{hash[:order].join(', ')}" unless hash[:order].blank?
66
- sql << "LIMIT #{hash[:limit]}" unless hash[:limit].blank?
67
- sql << "OFFSET #{hash[:offset]}" unless hash[:offset].blank?
68
-
69
- @@select % [ @table_name, sql.join(' ') ]
70
- end
71
- alias_method :to_sql, :to_s
72
-
73
- def keyed_clauses
74
- @clauses.inject({}) do |hash, clause|
75
- hash[clause.key] ||= []
76
- hash[clause.key] << clause.to_s
77
-
78
- if clause.respond_to?(:includes) && !clause.includes.blank?
79
- hash[:includes] ||= []
80
- hash[:includes] << clause.includes
81
- end
82
-
83
- hash
84
- end
85
- end
86
-
87
- def inspect
88
- "(Query object: call #to_sql or #to_hash to inspect, call an Enumerable (such as #each or #first) to request data)"
89
- end
90
- end
91
- end