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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, *col) ⇒ Column

Returns a new instance of Column.



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

def initialize model, *col
	@model = model
	@last_model = nil
	*@path, @col = Array.wrap( col).collect {|s| s.to_s.split /[.\/]/ }.flatten.collect &:to_sym
end

Instance Attribute Details

#colObject (readonly)

Returns the value of attribute col.



100
101
102
# File 'lib/smql_to_ar.rb', line 100

def col
  @col
end

#modelObject

Returns the value of attribute model.



101
102
103
# File 'lib/smql_to_ar.rb', line 101

def model
  @model
end

#pathObject (readonly)

Returns the value of attribute path.



100
101
102
# File 'lib/smql_to_ar.rb', line 100

def path
  @path
end

Instance Method Details

#allowed?Boolean

Returns:

  • (Boolean)


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

def allowed?()  ! self.protected?  end

#eachObject



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

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

#exist_in?Boolean

Returns:

  • (Boolean)


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

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

#inspectObject



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

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

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



140
141
142
143
144
145
146
147
148
# File 'lib/smql_to_ar.rb', line 140

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

#last_modelObject



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

def last_model
	@last_model ||= each{}
end

#protected?Boolean

Returns:

  • (Boolean)


129
130
131
132
133
134
135
136
137
138
# File 'lib/smql_to_ar.rb', line 129

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



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

def relation()  SmqlToAR.model_of last_model, @col  end

#to_aObject



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

def to_a()   @path+[@col]  end

#to_jsonObject



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

def to_json()  to_s  end

#to_sObject



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

def to_s()   to_a.join '.'  end

#to_symObject



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

def to_sym() to_s.to_sym  end