Module: ColumnGroup::ClassMethods

Included in:
Common::ClassMethods, Generic_Table::ClassMethods
Defined in:
app/models/column_group.rb

Constant Summary collapse

History_columns =

logical_primary_key

[:updated_at, :created_at, :id]

Instance Method Summary collapse

Instance Method Details

#attribute_ddl(attribute_name) ⇒ Object

logical_primary_key


41
42
43
44
45
# File 'app/models/column_group.rb', line 41

def attribute_ddl(attribute_name)
	table_sql= self.to_sql
	attribute_sql=table_sql.grep(attribute_name)
	return attribute_sql
end

#attribute_rails_type(attribute_name) ⇒ Object

attribute_ruby_type


50
51
52
# File 'app/models/column_group.rb', line 50

def attribute_rails_type(attribute_name)
	return first[attribute_name].class
end

#attribute_ruby_type(attribute_name) ⇒ Object

attribute_ddl


47
48
49
# File 'app/models/column_group.rb', line 47

def attribute_ruby_type(attribute_name)
	return first[attribute_name].class
end

#candidate_logical_keys_from_indexesObject

attribute_rails_type


53
54
55
56
57
58
59
60
61
62
# File 'app/models/column_group.rb', line 53

def candidate_logical_keys_from_indexes
	indexes=self.connection.indexes(self.name.tableize)
		if indexes != [] then
			indexes.map do |i|
				i.columns
			end #map
		else
			return nil
		end #if
end

#categorical?(attribute_name) ⇒ Boolean

probably_numerical

Returns:

  • (Boolean)

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/column_group.rb', line 90

def categorical?(attribute_name)
	if [Symbol].include?(attribute_ruby_type(attribute_name)) then
		return true
	elsif foreign_key_names.include?(attribute_name.to_s) then
		parent=association_class(foreign_key_to_association_name(attribute_name))
		return !parent.sequential_id?
	elsif defaulted_primary_logical_key? then
		if attribute_name.to_sym==:id then
			return logical_attributes==[]
		else
			return false
		end #if
	else #overridden logical primary key
		if attribute_name.to_sym==:id then
			return !sequential_id?
		else
			return logical_primary_key.include?(attribute_name)
		end #if
	end #if
end

#column_symbolsObject

probably_categorical


123
124
125
# File 'app/models/column_group.rb', line 123

def column_symbols
	return column_names.map {|name| name.to_sym}
end

#default_logical_primary_keyObject

defaulted_primary_logical_key


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/column_group.rb', line 18

def default_logical_primary_key
	if logical_attributes.include?(:name) then
		return [:name]
	else
		candidate_logical_primary_key=logical_attributes
		if !candidate_logical_primary_key.empty? then
			return candidate_logical_primary_key
		else
			model_history_type=history_type?
			if model_history_type==[] then
				raise "Can't find a default primary logical key in #{self.inspect}."
			else
				return model_history_type[0..0] # first prioritized column
			end #if
			return [:id]
		end #if
		return logical_attributes
	end #if
end

#defaulted_primary_logical_key?Boolean

Returns:

  • (Boolean)

11
12
13
14
15
16
17
# File 'app/models/column_group.rb', line 11

def defaulted_primary_logical_key?
	 if methods(false).include?('logical_primary_key') then
	 	return nil
	 else
	 	return true
	 end #if
end

#history_type?Boolean

Returns:

  • (Boolean)

151
152
153
154
155
156
157
# File 'app/models/column_group.rb', line 151

def history_type?
	history_type=[] # nothing yet
	History_columns.each do |history_column|
		history_type << history_column if column_symbols.include?(history_column) 
	end #each
	return history_type
end

#is_logical_primary_key?(attribute_names) ⇒ Boolean

logical_attributes

Returns:

  • (Boolean)

129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/models/column_group.rb', line 129

def is_logical_primary_key?(attribute_names)
	quoted_primary_key
	if self.respond_to?(:logical_primary_key) then
		if Set[logical_primary_key]==Set[attribute_names] then
			return true
		end #if
	end #if
	attribute_names.each do |attribute_name|
		if attribute_name=='id' then
			return false
		elsif !column_names.include(attribute_name.to_s) then
			return false
		end #IF	
	end #each
	if self.count==self.count(:distinct => true, :select => attribute_names) then
		return true
	else
		return false
	end #if
	return true # if we get here
end

#logical_attributesObject

column_symbols


126
127
128
# File 'app/models/column_group.rb', line 126

def logical_attributes
	return (column_symbols-History_columns).select {|name| !numerical?(name)} # avoid :id recursion
end

#logical_primary_keyObject

Override if default is wrong.


38
39
40
# File 'app/models/column_group.rb', line 38

def logical_primary_key
	return default_logical_primary_key
end

#logical_primary_key_recursiveObject

sequential_id


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/models/column_group.rb', line 163

def logical_primary_key_recursive
	if sequential_id? then
		return logical_primary_key
	else
		foreign_keys=logical_primary_key.map do |e| 
			if is_foreign_key_name?(e) then
				association_name=foreign_key_to_association_name(e)
				association=self.first.foreign_key_to_association(association_name)
				if association.nil? then
					nil
				else
					association.class.logical_primary_key_recursive
				end #if
			else
				e.to_sym
			end #if
		end #map
		{self.name => foreign_keys}
	end #if
end

#numerical?(attribute_name) ⇒ Boolean

Is attribute an numerical (analog) (versus categorical (digital) value) default logical primary keys ignore analog values Statistical procedures will treat these attributes as continuous override for specific classes by default the following are considered analog:

Float
Time
DateTime
id for sequential_id?

Returns:

  • (Boolean)

72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/column_group.rb', line 72

def numerical?(attribute_name)
	if ['created_at','updated_at'].include?(attribute_name.to_s) then
		return true
	elsif [Float, Bignum, DateTime, Time].include?(attribute_ruby_type(attribute_name)) then
		return true
	elsif categorical?(attribute_name) then
		return false
	else
		return false
	end #if
end

#probably_categorical?(attribute_name) ⇒ Boolean

categorical

Returns:

  • (Boolean)

110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/column_group.rb', line 110

def probably_categorical?(attribute_name)
	if [String, NilClass].include?(attribute_ruby_type(attribute_name)) then
		return true
	elsif attribute_name.to_sym==:id then
		if defaulted_primary_logical_key? then
			return logical_attributes==[]
		else #overridden logical primary key
			return logical_primary_key.include?(:id)
		end #if
	else
		return false
	end #if
end

#probably_numerical?(attribute_name) ⇒ Boolean

numerical

Returns:

  • (Boolean)

83
84
85
86
87
88
89
# File 'app/models/column_group.rb', line 83

def probably_numerical?(attribute_name)
	if [Date].include?(attribute_ruby_type(attribute_name)) then
		return true
	else
		return false
	end #if
end

#sequential_id?Boolean

history_type

Returns:

  • (Boolean)

158
159
160
161
162
# File 'app/models/column_group.rb', line 158

def sequential_id?
	history_types_not_in_logical_key= history_type?-logical_primary_key
	return history_types_not_in_logical_key!=history_type?

end