Class: SmqlToAR::ConditionTypes::Condition

Inherits:
Object
  • Object
show all
Extended by:
Assertion
Includes:
Assertion
Defined in:
lib/smql_to_ar/condition_types.rb

Constant Summary collapse

Operator =
nil
Expected =
[]
Where =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Assertion

raise_if, raise_unless

Constructor Details

#initialize(model, cols, val) ⇒ Condition

Returns a new instance of Condition.



114
115
116
117
118
119
120
121
122
# File 'lib/smql_to_ar/condition_types.rb', line 114

def initialize model, cols, val
  #p init: self, caller: caller
  @model, @cols = model, cols
  @value = case val
    when Hash, Range then val
    else Array.wrap val
    end
  verify
end

Instance Attribute Details

#colsObject (readonly)

Returns the value of attribute cols.



96
97
98
# File 'lib/smql_to_ar/condition_types.rb', line 96

def cols
  @cols
end

#valueObject (readonly)

Returns the value of attribute value.



96
97
98
# File 'lib/smql_to_ar/condition_types.rb', line 96

def value
  @value
end

Class Method Details

.inspectObject



109
110
111
# File 'lib/smql_to_ar/condition_types.rb', line 109

def inspect
  "#{self.name}( :operator=>#{self::Operator.inspect}, :expected=>#{self::Expected.inspect}, :where=>#{self::Where.inspect})"
end

.try_parse(model, cols, op, val) ⇒ Object

Versuche das Objekt zu erkennen. Operator und Expected muessen passen. Passt das Object, die Klasse instanzieren.



104
105
106
107
# File 'lib/smql_to_ar/condition_types.rb', line 104

def try_parse model, cols, op, val
  #p :class => self, :self => name, :try_parse => op, :cols => cols, :with => self::Operator, :value => val, :expected => self::Expected, :model => model.name
  new model, cols, val  if self::Operator === op and self::Expected.any? {|x| x === val}
end

Instance Method Details

#condition_build(builder, table) ⇒ Object Also known as: build

Erstelle alle noetigen Klauseln. builder nimmt diese entgegen, wobei builder.joins, builder.select, builder.where und builder.wobs von interesse sind. mehrere Schluessel bedeuten, dass die Values alle zutreffen muessen, wobei die Schluessel geODERt werden. Ex:

  1. "Peter" #=> givenname = 'Peter'
  2. ["Peter", "Hans"] #=> ( givenname = 'Peter' OR givenname = 'Hans' )
  3. ["Peter", "Mueller"] #=> ( givenname = 'Peter' OR surname = 'Peter' ) AND ( givenname = 'Mueller' OR surname = 'Mueller' )


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/smql_to_ar/condition_types.rb', line 156

def condition_build builder, table
  values = Hash[ @value.collect {|value| [ builder.vid, value ] } ]
  values.each {|k, v| builder.wobs k.to_sym => v }
  if 1 == @cols.length
    @cols.each do |col|
      col.joins builder, table
      col = builder.column table+col.path, col.col
      builder.where values.keys.collect {|vid| self.class::Where % [ col, vid.to_s ] }
    end
  else
    b2 = SmqlToAR::And.new builder
    values.keys.each do |vid|
      b2.where SmqlToAR::Or[ *@cols.collect {|col|
          col.joins builder, table
          col = builder.column table+col.path, col.col
          self.class::Where % [ col, vid.to_s ]
        }]
    end
  end
  self
end

#inspectObject



124
125
126
# File 'lib/smql_to_ar/condition_types.rb', line 124

def inspect
  "#<#{self.class.name}:0x#{(self.object_id<<1).to_s 16} model: #{self.class.name}, cols: #{@cols.inspect}, value: #{@value.inspect}>"
end

#verifyObject



128
129
130
131
132
133
# File 'lib/smql_to_ar/condition_types.rb', line 128

def verify
  @cols.each do |col|
    verify_column col
    verify_allowed col
  end
end

#verify_allowed(col) ⇒ Object

Modelle koennen Spalten/Relationen verbieten mit Model#smql_protected. Dieses muss ein Object mit #include?( name_als_string) zurueckliefern, welches true fuer verboten und false fuer, erlaubt steht.



144
145
146
# File 'lib/smql_to_ar/condition_types.rb', line 144

def verify_allowed col
  raise_if col.protected?, ProtectedColumnError.new( col)
end

#verify_column(col) ⇒ Object

Gibt es eine Spalte diesen Namens? Oder: Gibt es eine Relation diesen Namens? (Hier nicht der Fall)



137
138
139
# File 'lib/smql_to_ar/condition_types.rb', line 137

def verify_column col
  raise_unless col.exist_in?, NonExistingColumnError.new( %w[Column], col)
end