Class: SmqlToAR::Column

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/smql_to_ar.rb

Overview

Eine Spalte in einer Tabelle, relativ zu Column#model. Kann auch einen Pfad Column#path haben, wobei Column#col dann eine Relation von dem Model der letzten Relation von Column#path ist.

Defined Under Namespace

Classes: Col

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, *col) ⇒ Column

Returns a new instance of Column.



151
152
153
154
155
156
157
158
159
# File 'lib/smql_to_ar.rb', line 151

def initialize model, *col
  @model = model
  @last_model = nil
  *@path, @col = *Array.wrap( col).
      collect {|x| x.to_s.split /[.\/]/ }.
      flatten.
      collect( &Col.method( :new)).
      reject {|x| x === :self }
end

Instance Attribute Details

#colObject (readonly)

Returns the value of attribute col.



119
120
121
# File 'lib/smql_to_ar.rb', line 119

def col
  @col
end

#modelObject

Returns the value of attribute model.



120
121
122
# File 'lib/smql_to_ar.rb', line 120

def model
  @model
end

#pathObject (readonly)

Returns the value of attribute path.



119
120
121
# File 'lib/smql_to_ar.rb', line 119

def path
  @path
end

Instance Method Details

#allowed?Boolean

Returns:

  • (Boolean)


213
# File 'lib/smql_to_ar.rb', line 213

def allowed?()  ! self.protected?  end

#child?Boolean

Returns:

  • (Boolean)


214
# File 'lib/smql_to_ar.rb', line 214

def child?()  @path.empty? and !!relation  end

#eachObject



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/smql_to_ar.rb', line 165

def each
  model = @model
  @path.each do |rel|
    unless rel === :self
      model = SmqlToAR.model_of model, rel
      return false  unless model
      yield rel, model
    end
  end
  model
end

#exist_in?Boolean

Returns:

  • (Boolean)


177
178
179
180
181
# File 'lib/smql_to_ar.rb', line 177

def exist_in?
  model = last_model
  return false  unless model
  model.column_names.include? @col.to_s
end

#inspectObject



211
# File 'lib/smql_to_ar.rb', line 211

def inspect()  "#<Column: #{model} #{to_s}>"  end

#joins(builder = nil, table = nil, &exe) ⇒ Object



194
195
196
197
198
199
200
201
202
# File 'lib/smql_to_ar.rb', line 194

def joins builder = nil, table = nil, &exe
pp = []
table = Array.wrap table
exe ||= builder ? lambda {|j, m| builder.joins table+j, m} : Array.method( :[])
collect do |rel, model|
  pp.push rel
  exe.call pp, model
end
end

#last_modelObject



161
162
163
# File 'lib/smql_to_ar.rb', line 161

def last_model
  @last_model ||= each{}
end

#lengthObject



205
# File 'lib/smql_to_ar.rb', line 205

def length() @path.length+(self.self? ? 0 : 1)  end

#protected?Boolean

Returns:

  • (Boolean)


183
184
185
186
187
188
189
190
191
192
# File 'lib/smql_to_ar.rb', line 183

def protected?
  model = @model
  each do |rel, _model|
    pr = Array.wrap model.respond_to?( :smql_protected) ? model.smql_protected : nil
    pr.include? rel.to_s
    model = _model
  end
  pr = Array.wrap model.respond_to?( :smql_protected) ? model.smql_protected : nil
  pr.include? @col.to_s
end

#relationObject



212
# File 'lib/smql_to_ar.rb', line 212

def relation()  self.self? ? model : SmqlToAR.model_of( last_model, @col)  end

#self?Boolean

Returns:

  • (Boolean)


204
# File 'lib/smql_to_ar.rb', line 204

def self?()  !@col  end

#sizeObject



206
# File 'lib/smql_to_ar.rb', line 206

def size()   @path.size+(self.self? ? 0 : 1)  end

#to_aObject



207
# File 'lib/smql_to_ar.rb', line 207

def to_a()   @path+(self.self? ? [] : [@col])  end

#to_jsonObject



210
# File 'lib/smql_to_ar.rb', line 210

def to_json()  to_s  end

#to_sObject



208
# File 'lib/smql_to_ar.rb', line 208

def to_s()   to_a.join '.'  end

#to_symObject



209
# File 'lib/smql_to_ar.rb', line 209

def to_sym() to_s.to_sym  end