diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-08-12 14:55:31 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-08-12 14:55:31 -0700 |
commit | 196b4e05b7390ac31e12a17ae224b1cf35e4becd (patch) | |
tree | bc5a5e56f8311bd68feb628d89a04e845585f3b5 | |
parent | 1036749e394e5f7f039e75cdec7675f9ca5047b0 (diff) | |
download | rails-196b4e05b7390ac31e12a17ae224b1cf35e4becd.tar.gz rails-196b4e05b7390ac31e12a17ae224b1cf35e4becd.tar.bz2 rails-196b4e05b7390ac31e12a17ae224b1cf35e4becd.zip |
tables can fetch attributes
-rw-r--r-- | lib/arel.rb | 3 | ||||
-rw-r--r-- | lib/arel/attributes.rb | 20 | ||||
-rw-r--r-- | lib/arel/attributes/attribute.rb | 13 | ||||
-rw-r--r-- | lib/arel/table.rb | 17 | ||||
-rw-r--r-- | spec/arel/attributes_spec.rb | 41 | ||||
-rw-r--r-- | spec/arel/table_spec.rb | 12 |
6 files changed, 100 insertions, 6 deletions
diff --git a/lib/arel.rb b/lib/arel.rb index a805cf60fa..afab11cd91 100644 --- a/lib/arel.rb +++ b/lib/arel.rb @@ -1,3 +1,6 @@ require 'arel/version' require 'arel/table' +require 'arel/attributes' + +# below is deprecated require 'arel/sql/engine' diff --git a/lib/arel/attributes.rb b/lib/arel/attributes.rb new file mode 100644 index 0000000000..a7fb00293b --- /dev/null +++ b/lib/arel/attributes.rb @@ -0,0 +1,20 @@ +require 'arel/attributes/attribute' + +module Arel + module Attributes + ### + # Factory method to wrap a raw database +column+ to an Arel Attribute. + def self.for column + case column.type + when :string, :text, :binary then String + when :integer then Integer + when :float then Float + when :decimal then Decimal + when :date, :datetime, :timestamp, :time then Time + when :boolean then Boolean + else + raise NotImplementedError, "Column type `#{column.type}` is not currently handled" + end + end + end +end diff --git a/lib/arel/attributes/attribute.rb b/lib/arel/attributes/attribute.rb new file mode 100644 index 0000000000..b83a32bee7 --- /dev/null +++ b/lib/arel/attributes/attribute.rb @@ -0,0 +1,13 @@ +module Arel + module Attributes + class Attribute < Struct.new :relation, :name, :column + end + + class String < Attribute; end + class Time < Attribute; end + class Boolean < Attribute; end + class Decimal < Attribute; end + class Float < Attribute; end + class Integer < Attribute; end + end +end diff --git a/lib/arel/table.rb b/lib/arel/table.rb index a4ae9bd18d..78b3ea9933 100644 --- a/lib/arel/table.rb +++ b/lib/arel/table.rb @@ -3,13 +3,20 @@ module Arel @engine = nil class << self; attr_accessor :engine; end - def initialize table_name, engine = Table.engine - @table_name = table_name - @engine = engine + def initialize name, engine = Table.engine + @name = name + @engine = engine end - def [] attribute - raise attribute + def columns + @engine.connection.columns(@name, "#{@name} Columns").map do |column| + Attributes.for(column).new self, column.name, column + end + end + + def [] name + name = name.to_s + columns.find { |column| column.name == name } end end end diff --git a/spec/arel/attributes_spec.rb b/spec/arel/attributes_spec.rb new file mode 100644 index 0000000000..8b437dff9b --- /dev/null +++ b/spec/arel/attributes_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +module Arel + describe 'Attributes' do + describe 'for' do + it 'returns the correct constant for strings' do + [:string, :text, :binary].each do |type| + column = Struct.new(:type).new type + Attributes.for(column).should == Attributes::String + end + end + + it 'returns the correct constant for ints' do + column = Struct.new(:type).new :integer + Attributes.for(column).should == Attributes::Integer + end + + it 'returns the correct constant for floats' do + column = Struct.new(:type).new :float + Attributes.for(column).should == Attributes::Float + end + + it 'returns the correct constant for decimals' do + column = Struct.new(:type).new :decimal + Attributes.for(column).should == Attributes::Decimal + end + + it 'returns the correct constant for boolean' do + column = Struct.new(:type).new :boolean + Attributes.for(column).should == Attributes::Boolean + end + + it 'returns the correct constant for time' do + [:date, :datetime, :timestamp, :time].each do |type| + column = Struct.new(:type).new type + Attributes.for(column).should == Attributes::Time + end + end + end + end +end diff --git a/spec/arel/table_spec.rb b/spec/arel/table_spec.rb index 20c962c26a..c52a111489 100644 --- a/spec/arel/table_spec.rb +++ b/spec/arel/table_spec.rb @@ -6,14 +6,24 @@ module Arel @relation = Table.new(:users) end + describe 'columns' do + it 'returns a list of columns' do + columns = @relation.columns + columns.length.should == 2 + columns.map { |x| x.name }.sort.should == %w{ name id }.sort + end + end + describe '[]' do describe 'when given a', Symbol do it "manufactures an attribute if the symbol names an attribute within the relation" do column = @relation[:id] - #.should == Attributes::Integer.new(@relation, :id) + column.name.should == 'id' + column.should be_kind_of Attributes::Integer end end + ### FIXME: this seems like a bad requirement. #describe 'when given an', Attribute do # it "returns the attribute if the attribute is within the relation" do # @relation[@relation[:id]].should == @relation[:id] |