Class: AsyncRecord::Base
- Inherits:
-
Object
- Object
- AsyncRecord::Base
- Defined in:
- lib/async_record/base.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- DEFAULT_PRIMARY_KEY =
"id"
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
Class Method Summary collapse
-
.all(options = {}, &blk) ⇒ Object
Selects all records from the table.
-
.connection ⇒ Object
Returns the current connection for AsyncRecord.
-
.count(&blk) ⇒ Object
Gets the count of records in the table.
-
.find(id, &blk) ⇒ Object
Find by primary key.
- .inherited(klass) ⇒ Object
-
.query(queryStr, &blk) ⇒ Object
Execute a query.
- .set_connection(conn) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #method_missing(method, *args) ⇒ Object
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
12 13 14 |
# File 'lib/async_record/base.rb', line 12 def initialize self.attributes = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/async_record/base.rb', line 20 def method_missing(method, *args) # Try to lookup the property return self.attributes[method.to_s] if self.attributes.has_key? method.to_s super end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
3 4 5 |
# File 'lib/async_record/base.rb', line 3 def attributes @attributes end |
Class Method Details
.all(options = {}, &blk) ⇒ Object
Selects all records from the table
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/async_record/base.rb', line 35 def self.all(={}, &blk) limit = '' limit = "limit #{[:limit]}" if .has_key? :limit connection.query("select * from #{@table_name} #{limit}") do |results| items = [] results.each do |result| obj = self.new obj.attributes = result items << obj end blk.call(items) end end |
.connection ⇒ Object
Returns the current connection for AsyncRecord.
74 75 76 |
# File 'lib/async_record/base.rb', line 74 def self.connection AsyncRecord::Base.instance_variable_get(:@conn) end |
.count(&blk) ⇒ Object
Gets the count of records in the table.
67 68 69 70 71 |
# File 'lib/async_record/base.rb', line 67 def self.count(&blk) connection.query("select count(*) from #{@table_name}") do |result| blk.call(result[0]["count(*)"]) end end |
.find(id, &blk) ⇒ Object
Find by primary key
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/async_record/base.rb', line 54 def self.find(id, &blk) connection.query("select * from #{@table_name} where #{@primary_key} = #{id.to_i}") do |result| if result.length == 1 obj = self.new obj.attributes = result[0] blk.call(obj) else blk.call(nil) end end end |
.inherited(klass) ⇒ Object
7 8 9 10 |
# File 'lib/async_record/base.rb', line 7 def self.inherited(klass) klass.extend(ClassMethods) setup_class(klass) end |
.query(queryStr, &blk) ⇒ Object
Execute a query
28 29 30 31 32 |
# File 'lib/async_record/base.rb', line 28 def self.query(queryStr, &blk) connection.query(queryStr) do |results| blk.call(results) end end |
.set_connection(conn) ⇒ Object
16 17 18 |
# File 'lib/async_record/base.rb', line 16 def self.set_connection(conn) @conn = conn end |