Class: SmqlToAR
- Inherits:
-
Object
- Object
- SmqlToAR
- Includes:
- ActiveSupport::Benchmarkable
- Defined in:
- lib/smql_to_ar.rb,
lib/smql_to_ar/query_builder.rb,
lib/smql_to_ar/condition_types.rb
Overview
TODO:
-
Array als Typ ist zu allgemein. Allgemeine Lösung gesucht, um die Typen im Array angeben zu können.
Defined Under Namespace
Modules: ActiveRecordExtensions, Assertion, ConditionTypes Classes: And, BuilderError, Column, ConColumnError, NonExistingColumnError, NonExistingRelationError, NonExistingSelectableError, Or, ParseError, ProtectedColumnError, QueryBuilder, Railtie, RootOnlyFunctionError, SMQLError, SubBuilder, SubSMQLError, UnexpectedColOpError, UnexpectedError
Constant Summary collapse
- @@logger =
::Rails.logger || begin require 'logger'; Logger.new( $stdout) end
Instance Attribute Summary collapse
-
#builder ⇒ Object
readonly
Returns the value of attribute builder.
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Class Method Summary collapse
- .logger ⇒ Object
- .logger=(logger) ⇒ Object
-
.model_of(model, rel) ⇒ Object
Model der Relation ‘rel` von `model`.
- .models(models) ⇒ Object
- .reload_library ⇒ Object
- .to_ar(*params) ⇒ Object
Instance Method Summary collapse
- #ar ⇒ Object
- #build ⇒ Object
-
#initialize(model, query, order = nil) ⇒ SmqlToAR
constructor
A new instance of SmqlToAR.
- #parse ⇒ Object
- #to_ar ⇒ Object
Constructor Details
#initialize(model, query, order = nil) ⇒ SmqlToAR
Returns a new instance of SmqlToAR.
233 234 235 236 237 |
# File 'lib/smql_to_ar.rb', line 233 def initialize model, query, order = nil query = JSON.parse query if query.kind_of? String @model, @query, @logger, @order = model, query, @@logger, order #p model: @model, query: @query end |
Instance Attribute Details
#builder ⇒ Object (readonly)
Returns the value of attribute builder.
217 218 219 |
# File 'lib/smql_to_ar.rb', line 217 def builder @builder end |
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
217 218 219 |
# File 'lib/smql_to_ar.rb', line 217 def conditions @conditions end |
#logger ⇒ Object
Returns the value of attribute logger.
218 219 220 |
# File 'lib/smql_to_ar.rb', line 218 def logger @logger end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
217 218 219 |
# File 'lib/smql_to_ar.rb', line 217 def model @model end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
217 218 219 |
# File 'lib/smql_to_ar.rb', line 217 def order @order end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
217 218 219 |
# File 'lib/smql_to_ar.rb', line 217 def query @query end |
Class Method Details
.logger ⇒ Object
230 |
# File 'lib/smql_to_ar.rb', line 230 def logger() @@logger end |
.logger=(logger) ⇒ Object
229 |
# File 'lib/smql_to_ar.rb', line 229 def logger=(logger) @@logger = logger end |
.model_of(model, rel) ⇒ Object
Model der Relation ‘rel` von `model`
109 110 111 112 |
# File 'lib/smql_to_ar.rb', line 109 def self.model_of model, rel r = model.reflections[ rel.to_sym].andand.klass r.nil? && rel === :self ? model : r end |
.models(models) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/smql_to_ar.rb', line 239 def self.models models models = Array.wrap models r = Hash.new {|h,k| h[k] = {} } while model = models.tap( &:uniq!).pop refls = model.respond_to?( :reflections) && model.reflections refls && refls.each do |name, refl| r[model.name][name] = case refl when ActiveRecord::Reflection::ThroughReflection then {macro: refl.macro, model: refl.klass.name, through: refl.through_reflection.name} when ActiveRecord::Reflection::AssociationReflection then {macro: refl.macro, model: refl.klass.name} else raise "Ups: #{refl.class}" end models.push refl.klass unless r.keys.include? refl.klass.name end end r end |
.reload_library ⇒ Object
292 293 294 295 296 297 298 299 |
# File 'lib/smql_to_ar.rb', line 292 def self.reload_library lib_dir = File.dirname __FILE__ fj = lambda {|*a| File.join lib_dir, *a } Object.send :remove_const, :SmqlToAR load fj[ 'smql_to_ar.rb'] load fj[ 'smql_to_ar', 'condition_types.rb'] load fj[ 'smql_to_ar', 'query_builder.rb'] end |
.to_ar(*params) ⇒ Object
288 289 290 |
# File 'lib/smql_to_ar.rb', line 288 def self.to_ar *params new( *params).to_ar end |
Instance Method Details
#ar ⇒ Object
274 275 276 277 278 |
# File 'lib/smql_to_ar.rb', line 274 def ar @ar ||= #benchmark 'SMQL ar' do @builder.to_ar #end end |
#build ⇒ Object
264 265 266 267 268 269 270 271 272 |
# File 'lib/smql_to_ar.rb', line 264 def build #benchmark 'SMQL build query' do @builder = QueryBuilder.new @model table = @builder.base_table @conditions.each {|condition| condition.build builder, table } #end #p builder: @builder self end |
#parse ⇒ Object
256 257 258 259 260 261 262 |
# File 'lib/smql_to_ar.rb', line 256 def parse #benchmark 'SMQL parse' do @conditions = ConditionTypes.try_parse @model, @query #end #p conditions: @conditions self end |
#to_ar ⇒ Object
280 281 282 283 284 285 286 |
# File 'lib/smql_to_ar.rb', line 280 def to_ar #benchmark 'SMQL' do parse build ar #end end |