Class: SmqlToAR

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#builderObject (readonly)

Returns the value of attribute builder.



217
218
219
# File 'lib/smql_to_ar.rb', line 217

def builder
  @builder
end

#conditionsObject (readonly)

Returns the value of attribute conditions.



217
218
219
# File 'lib/smql_to_ar.rb', line 217

def conditions
  @conditions
end

#loggerObject

Returns the value of attribute logger.



218
219
220
# File 'lib/smql_to_ar.rb', line 218

def logger
  @logger
end

#modelObject (readonly)

Returns the value of attribute model.



217
218
219
# File 'lib/smql_to_ar.rb', line 217

def model
  @model
end

#orderObject (readonly)

Returns the value of attribute order.



217
218
219
# File 'lib/smql_to_ar.rb', line 217

def order
  @order
end

#queryObject (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

.loggerObject



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_libraryObject



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

#arObject



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

#buildObject



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

#parseObject



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_arObject



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