diff options
Diffstat (limited to 'lib/arel')
-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 |
3 files changed, 45 insertions, 5 deletions
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 |