diff options
author | Bryan Helmkamp <bryan@brynary.com> | 2009-05-17 13:58:29 -0400 |
---|---|---|
committer | Bryan Helmkamp <bryan@brynary.com> | 2009-05-17 13:58:29 -0400 |
commit | 2bbf8ca9d2af3ea959a21c3729b4894bc31f088b (patch) | |
tree | d28994667dbebe135cc7c43d3948a5e5d968f214 /lib | |
parent | 49d119ae84bbb7cd180ca855cf48997dc731554c (diff) | |
download | rails-2bbf8ca9d2af3ea959a21c3729b4894bc31f088b.tar.gz rails-2bbf8ca9d2af3ea959a21c3729b4894bc31f088b.tar.bz2 rails-2bbf8ca9d2af3ea959a21c3729b4894bc31f088b.zip |
reorganized call
Conflicts:
doc/TODO
lib/arel/relations/relation.rb
lib/arel/relations/writes/delete.rb
lib/arel/relations/writes/insert.rb
lib/arel/relations/writes/update.rb
lib/arel/session.rb
spec/arel/unit/relations/delete_spec.rb
spec/arel/unit/relations/insert_spec.rb
spec/arel/unit/relations/relation_spec.rb
spec/arel/unit/relations/update_spec.rb
spec/arel/unit/session/session_spec.rb
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/engine.rb | 24 | ||||
-rw-r--r-- | lib/arel/relations.rb | 4 | ||||
-rw-r--r-- | lib/arel/relations/array.rb | 19 | ||||
-rw-r--r-- | lib/arel/relations/relation.rb | 9 | ||||
-rw-r--r-- | lib/arel/relations/writes/delete.rb | 4 | ||||
-rw-r--r-- | lib/arel/relations/writes/insert.rb | 4 | ||||
-rw-r--r-- | lib/arel/relations/writes/update.rb | 4 | ||||
-rw-r--r-- | lib/arel/session.rb | 8 |
8 files changed, 56 insertions, 20 deletions
diff --git a/lib/arel/engine.rb b/lib/arel/engine.rb index 5a5ef06878..2bb4bbbd90 100644 --- a/lib/arel/engine.rb +++ b/lib/arel/engine.rb @@ -14,5 +14,29 @@ module Arel def method_missing(method, *args, &block) @ar.connection.send(method, *args, &block) end + + module CRUD + def create(relation) + connection.insert(relation.to_sql) + end + + def read(relation) + results = connection.execute(relation.to_sql) + rows = [] + results.each do |row| + rows << attributes.zip(row).to_hash + end + rows + end + + def update(relation) + connection.update(relation.to_sql) + end + + def delete(relation) + connection.delete(relation.to_sql) + end + end + include CRUD end end diff --git a/lib/arel/relations.rb b/lib/arel/relations.rb index 3394fac7cb..fd758ed15d 100644 --- a/lib/arel/relations.rb +++ b/lib/arel/relations.rb @@ -1,8 +1,6 @@ require 'arel/relations/relation' - require 'arel/relations/utilities' - require 'arel/relations/table' - +require 'arel/relations/array' require 'arel/relations/writes' require 'arel/relations/operations'
\ No newline at end of file diff --git a/lib/arel/relations/array.rb b/lib/arel/relations/array.rb new file mode 100644 index 0000000000..dac65abbc2 --- /dev/null +++ b/lib/arel/relations/array.rb @@ -0,0 +1,19 @@ +module Arel + class Array < Relation + include Recursion::BaseCase + + def initialize(array, attribute_names) + @array, @attribute_names = array, attribute_names + end + + def attributes + @attributes ||= @attribute_names.collect do |name| + Attribute.new(self, name.to_sym) + end + end + + def call(connection = nil) + @array.collect { |row| attributes.zip(row).to_hash } + end + end +end
\ No newline at end of file diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index 5d2c336a15..ef295dfdd7 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -32,13 +32,8 @@ module Arel "IN" end - def call(connection = engine) - results = connection.execute(to_sql) - rows = [] - results.each do |row| - rows << attributes.zip(row).to_hash - end - rows + def call + engine.read(self) end def bind(relation) diff --git a/lib/arel/relations/writes/delete.rb b/lib/arel/relations/writes/delete.rb index b1ff3bef27..0a04454e7f 100644 --- a/lib/arel/relations/writes/delete.rb +++ b/lib/arel/relations/writes/delete.rb @@ -12,8 +12,8 @@ module Arel ].compact.join("\n") end - def call(connection = engine) - connection.delete(to_sql) + def call + engine.delete(self) end end end diff --git a/lib/arel/relations/writes/insert.rb b/lib/arel/relations/writes/insert.rb index d579ad06d0..d05bd9cdc8 100644 --- a/lib/arel/relations/writes/insert.rb +++ b/lib/arel/relations/writes/insert.rb @@ -16,8 +16,8 @@ module Arel ].join("\n") end - def call(connection = engine) - connection.insert(to_sql) + def call + engine.create(self) end end end diff --git a/lib/arel/relations/writes/update.rb b/lib/arel/relations/writes/update.rb index 2e90de3a52..fd0aadb479 100644 --- a/lib/arel/relations/writes/update.rb +++ b/lib/arel/relations/writes/update.rb @@ -18,8 +18,8 @@ module Arel ].join("\n") end - def call(connection = engine) - connection.update(to_sql) + def call + engine.update(self) end end end diff --git a/lib/arel/session.rb b/lib/arel/session.rb index d9a6e4b5e4..9c2ddc535b 100644 --- a/lib/arel/session.rb +++ b/lib/arel/session.rb @@ -25,21 +25,21 @@ module Arel module CRUD def create(insert) - insert.call(insert.engine) + insert.call end def read(select) (@read ||= Hash.new do |hash, select| - hash[select] = select.call(select.engine) + hash[select] = select.call end)[select] end def update(update) - update.call(update.engine) + update.call end def delete(delete) - delete.call(delete.engine) + delete.call end end include CRUD |