Module: Mysql2Model::Container::ClassMethods
- Defined in:
- lib/mysql2_model/container.rb
Instance Method Summary collapse
-
#client ⇒ Object
Your models repository client via the cache.
-
#default_repository_name ⇒ Object
abstract
Define which repository or repositories act as the destination(s) of your model.
-
#query(statement = '', *args) ⇒ Object
(also: #execute)
Flavor the behavior by returning the resultset as instances of self instead of Mysql2::Result.
-
#value(statement = '', *args) ⇒ Object, Array
Useful with queries that only return one result, like a COUNT.
-
#value_sum(statement = '', *args) ⇒ Object
The sum of adding the first value from each of the rows primarily useful with count queries running against multiple repos.
- #with_composed_sql(statement = '', *args) {|composed_sql| ... } ⇒ Object
Instance Method Details
#client ⇒ Object
Your models repository client via the cache
101 102 103 |
# File 'lib/mysql2_model/container.rb', line 101 def client Mysql2Model::Client[default_repository_name] end |
#default_repository_name ⇒ Object
Should probably rename this to just default_repository since Array != Symbol or just “repository”; “default_repository_name” came from Datamapper
Define which repository or repositories act as the destination(s) of your model.
96 97 98 |
# File 'lib/mysql2_model/container.rb', line 96 def default_repository_name :default end |
#query(statement = '', *args) ⇒ Object Also known as: execute
Flavor the behavior by returning the resultset as instances of self instead of Mysql2::Result
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/mysql2_model/container.rb', line 144 def query(statement='',*args) statement = yield if block_given? with_composed_sql(statement,*args) do |composed_sql| response = client.query(composed_sql) if response.respond_to?(:map) response.map do |row| # @todo Patch Mysql2 to support loading the primitives directly into custom class :as => CustomClass, and remove this map # @todo This is defeating Mysql2's lazy loading, but it's good for proof-of-concept self.new(row) end else response end end end |
#value(statement = '', *args) ⇒ Object, Array
Remove the block pattern, we will need to utilize the block for the evented query pattern
Useful with queries that only return one result, like a COUNT.
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/mysql2_model/container.rb', line 132 def value(statement='',*args) statement = yield if block_given? with_composed_sql(statement,*args) do |composed_sql| if (rv = client.query(composed_sql)).count > 1 rv.map {|row| row.first.last } else rv.first.first.last end end end |
#value_sum(statement = '', *args) ⇒ Object
considering renaming value_sum => value, value => value_for_each, but what happens if value is a String instead of Numeric?
Remove the block pattern, we will need to utilize the block for the evented query pattern
Returns the sum of adding the first value from each of the rows primarily useful with count queries running against multiple repos.
119 120 121 122 123 124 125 126 |
# File 'lib/mysql2_model/container.rb', line 119 def value_sum(statement='',*args) statement = yield if block_given? with_composed_sql(statement,*args) do |sql| client.query(sql).inject(0) { |sum,row| sum += row.first.last } end end |
#with_composed_sql(statement = '', *args) {|composed_sql| ... } ⇒ Object
109 110 111 112 113 |
# File 'lib/mysql2_model/container.rb', line 109 def with_composed_sql(statement='',*args) composed_sql = compose_sql(statement,*args).strip log.info("SQL:[#{composed_sql}]") yield composed_sql end |