Class: DataMapper::SQLFinders::TableRepresentation

Inherits:
Object
  • Object
show all
Defined in:
lib/data_mapper/sql_finders/table_representation.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, idx = 0) ⇒ TableRepresentation

Returns a new instance of TableRepresentation.



14
15
16
17
# File 'lib/data_mapper/sql_finders/table_representation.rb', line 14

def initialize(model, idx = 0)
  @model = model
  @idx   = idx
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/data_mapper/sql_finders/table_representation.rb', line 36

def method_missing(name, *args, &block)
  return super unless args.size == 0 && !block_given?

  if property = @model.properties[name]
    "#{storage_alias}.#{@model.repository.adapter.send(:quote_name, property.field)}"
  elsif @model.method_defined?(name)
    @model.repository.adapter.send(:quote_name, name)
  else
    super
  end
end

Class Method Details

.from_models(*models) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/data_mapper/sql_finders/table_representation.rb', line 5

def from_models(*models)
  seen = {}
  models.map do |model|
    seen[model] ||= -1
    new(model, seen[model] += 1)
  end
end

Instance Method Details

#*Object



32
33
34
# File 'lib/data_mapper/sql_finders/table_representation.rb', line 32

def *
  @model.properties.map { |p| "#{storage_alias}.#{@model.repository.adapter.send(:quote_name, p.field)}" }.join(", ")
end

#storage_aliasObject



25
26
27
28
29
30
# File 'lib/data_mapper/sql_finders/table_representation.rb', line 25

def storage_alias
  name = @model.storage_name.dup.tap do |name|
    name << "_#{@idx}" if @idx > 0
  end
  @model.repository.adapter.send(:quote_name, name)
end

#to_sObject



19
20
21
22
23
# File 'lib/data_mapper/sql_finders/table_representation.rb', line 19

def to_s
  @model.repository.adapter.send(:quote_name, @model.storage_name).dup.tap do |tbl|
    tbl << " AS #{storage_alias}" unless tbl == storage_alias
  end
end