Module: Generic_Table::ClassMethods

Constant Summary

Constants included from ColumnGroup::ClassMethods

ColumnGroup::ClassMethods::History_columns

Constants included from GenericGrep::ClassMethods

GenericGrep::ClassMethods::ASSOCIATION_MACRO_LETTERS, GenericGrep::ClassMethods::ASSOCIATION_MACRO_PATTERN

Instance Method Summary collapse

Methods included from ColumnGroup::ClassMethods

#attribute_ddl, #attribute_rails_type, #attribute_ruby_type, #candidate_logical_keys_from_indexes, #categorical?, #column_symbols, #default_logical_primary_key, #defaulted_primary_logical_key?, #history_type?, #is_logical_primary_key?, #logical_attributes, #logical_primary_key, #logical_primary_key_recursive, #numerical?, #probably_categorical?, #probably_numerical?, #sequential_id?

Methods included from GenericGrep::ClassMethods

#all_associations, #association_grep, #association_grep_pattern, #association_macro_type, #association_type, #belongs_to_association?, #grep_all_associations_command, #grep_command, #has_many_association?, #has_one_association?, #model_grep, #model_grep_command

Methods included from GenericTableHtml::ClassMethods

#column_order, #header_html, #table_html

Instance Method Details

#is_active_record_method?(method_name) ⇒ Boolean

To detect collisions between attributes as methods and ActiveRecord methods.

Returns:

  • (Boolean)

136
137
138
139
140
141
142
# File 'app/models/generic_table.rb', line 136

def is_active_record_method?(method_name)
	if ActiveRecord::Base.instance_methods_from_class(true).include?(method_name.to_s) then
		return true
	else
		return false
	end #if
end

#model_file_nameObject

sample


60
61
62
# File 'app/models/generic_table.rb', line 60

def model_file_name
	return "app/models/#{name.tableize.singularize}.rb"
end

#nestingObject


21
22
23
# File 'app/models/generic_table.rb', line 21

def nesting
	return Module.nesting
end

#one_pass_statistics(column_name) ⇒ Object

Whether primay logical key has been overridden or ActiveRecord::Base.logical_primary_key is used. nil returned if overridden. true returned otherwise. from en.wikipedia.org/wiki/Algorithms_for_calculating_variance Skewness is defined at en.wikipedia.org/wiki/Skewness kurtosis is defined at en.wikipedia.org/wiki/Kurtosis


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/generic_table.rb', line 70

def one_pass_statistics(column_name)
    n = 0
    mean = 0
    m2 = 0
    m3 = 0
    m4 = 0
    min=nil; max=nil
	max_key, min_key = nil # declare scope outside loop!
    has_id=column_names.include?('id')
    all.each do |row|
        x=row[column_name]
        n1 = n
        n = n + 1
        delta = x - mean
        delta_n = delta / n
        delta_n2 = delta_n * delta_n
        term1 = delta * delta_n * n1
        mean = mean + delta_n
        if n==1 then
	    min=x # value for nil
	    max=x # value for nil
	    if has_id then
		    min_key=row.id
		    max_key=row.id
	    else
		    min_key=row.logical_primary_key_value_recursive
		    max_key=row.logical_primary_key_value_recursive
	    end #if
	else
            m4 = m4 + term1 * delta_n2 * (n*n - 3*n + 3) + 6 * delta_n2 * m2 - 4 * delta_n * m3
            m3 = m3 + term1 * delta_n * (n - 2) - 3 * delta_n * m2
            m2 = m2 + delta*(x - mean)
	    if x<min then
	    	min=x
		if has_id then
		    min_key=row.id
	    	else
		    min_key=row.logical_primary_key_value_recursive
	    	end #if
	    end #if  # value for not nil
	    if x>max then
	    	max=x
		if has_id then
			max_key=row.id
		else
			max_key=row.logical_primary_key_value_recursive
		end #if
	    end #if  # value for not nil
	end #if
    end #each
    return nil if n==0
    {
    :n => n,
    :variance_n => m2/n,
    :variance => m2/(n - 1), 
    :skewness=> Math::sqrt(n)*m3/(m2**(3/2)),
    :kurtosis => (n*m4) / (m2*m2) - 3,
    :min => min,
    :min_key => min_key,
    :mean => mean,
    :max => max,
    :max_key => max_key,
    :has_id => has_id
    }
end

#sample(samples_wanted = 100, sample_type = :first, consecutive = 1) ⇒ Object

return a statified or random sample returns a nested array of sample records Usually you will want sample.flatten The nested structure is available for plotting (say different colors) to see locality and trends.


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/generic_table.rb', line 41

def sample(samples_wanted=100, sample_type=:first, consecutive=1)
	size=all.size
	samples_returned=[samples_wanted, size].min
	bursts=(samples_returned/consecutive).ceil
	spacing=(size/bursts).ceil
	ret=(0..bursts-1).map do |burst|
		burst_start=burst*spacing
		case sample_type
		when :first, :last
			sample_burst(sample_type, burst_start, spacing, consecutive)
		when :random
			burst_start=rand(samples_returned)
			sample_burst(sample_type, burst_start, spacing, consecutive)
		else
			raise "Unknown sample type=#{sample_type}. Expected values are :first, :random, :last"
		end #case
	end #map burst
	return ret #[0..samples_returned-1]
end

#sample_burst(sample_type, start, spacing, consecutive) ⇒ Object

nesting


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/generic_table.rb', line 24

def sample_burst(sample_type, start, spacing, consecutive)
	if consecutive>spacing then
		raise "consecutive(#{consecutive})>spacing(#{spacing})"
	end #if
	case sample_type
	when :first, :random
		return all[start, consecutive]
	when :last
		return all[start+spacing-consecutive, consecutive]
	else
		raise "Unknown sample type=#{sample_type}. Expected values are :first, :Last."
	end #case
end