Class: Slick::Database::Union

Inherits:
Object
  • Object
show all
Extended by:
Registry
Defined in:
lib/slick/database/union.rb

Instance Attribute Summary

Attributes included from Registry

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Registry

create, for, register, registered?, registered_classes

Constructor Details

#initialize(database, collections = nil) ⇒ Union

Returns a new instance of Union.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/slick/database/union.rb', line 23

def initialize(database, collections = nil)
    @database = database
    @tables = []
    (collections || self.class.table_classes.map{|table_class| table_class.new(@database)}).each do |table|
        if table.kind_of?(Slick::Database::Union)
            @tables.concat(table.instance_variable_get(:@tables))
        elsif table.kind_of?(Slick::Database::Table)
            @tables << table
        end
    end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



55
56
57
# File 'lib/slick/database/union.rb', line 55

def method_missing(*args, &block)
    Slick::Database::Union.new(@database, @tables.map{|table| table.send(*args, &block)})
end

Class Method Details

.register(name, *args) ⇒ Object



10
11
12
13
14
15
# File 'lib/slick/database/union.rb', line 10

def register(name, *args)
    super
    Class.new(Slick::Helper){ register name }.define_method "call" do |*args, &block|
        database.send(self.class.name, *args, &block)
    end
end

.table_classesObject



17
18
19
# File 'lib/slick/database/union.rb', line 17

def table_classes
    @table_classes ||= []
end

Instance Method Details

#+(collection) ⇒ Object



35
36
37
# File 'lib/slick/database/union.rb', line 35

def +(collection)
    Slick::Database::Union.new(@database, @tables.clone.push(collection))
end

#all(options = {}) ⇒ Object



39
40
41
# File 'lib/slick/database/union.rb', line 39

def all(options = {})
    @database.run(_generate_select_sql(options), options)
end

#each(options = {}, &block) ⇒ Object



43
44
45
# File 'lib/slick/database/union.rb', line 43

def each(options = {}, &block)
    @database.run(_generate_select_sql(options), options).each(&block)
end

#explain(options = {}) ⇒ Object



47
48
49
# File 'lib/slick/database/union.rb', line 47

def explain(options = {})
    @database.send(:_prepare, _generate_select_sql(options))
end

#inspectObject



51
52
53
# File 'lib/slick/database/union.rb', line 51

def inspect
    all
end