Class: NativeQuery::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/native-query/model.rb

Overview

Represents instance of ORM model.

Constant Summary collapse

RELEVANT_METHODS =

Indicates relevant methods for binding to the connection object.

Set::new [
    :insert, :update, :delete, :begin, :commit, :rollback, :transaction
]

Instance Method Summary collapse

Constructor Details

#initialize(driver, configuration) ⇒ Model

Constructor.



46
47
48
49
# File 'lib/native-query/model.rb', line 46

def initialize(driver, configuration)
    @driver = driver
    @configuration = configuration
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Maps missing calls to tables.

Arguments are expected to be field names, so given to field query method.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/native-query/model.rb', line 70

def method_missing(sym, *args, &block)
    
    # If it's binding to the connection
    if sym.in? Model::RELEVANT_METHODS
        return self.connection.send(sym, *args, &block)
        
    # In otherwise, it's query request
    else
        query = Query::new(self.connection, sym)
        
        if args and not args.empty?
            query.fields(*args)
        end
        
        if not block.nil?
            result = query.instance_eval(&block)
        else
            result = query
        end

        return result
    end
    
end

Instance Method Details

#connectionObject

Returns connection.



55
56
57
58
59
60
61
# File 'lib/native-query/model.rb', line 55

def connection
    if not @connection
        @connection = FluentQuery::Connection::new(@driver, @configuration) 
    end
    
    @connection    # returns
end