Module: ChupakabraTools::ClassHelper

Defined in:
lib/chupakabra_tools/class_helper.rb

Class Method Summary collapse

Class Method Details

.attribute_required?(model, attribute) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/chupakabra_tools/class_helper.rb', line 96

def self.attribute_required?(model, attribute)

    if model && attribute
        validators = model.class.validators_on(attribute)
        validators.each do |v|
            if v.is_a?(ActiveModel::Validations::PresenceValidator)
                return true
            end
        end
    end
    false
end

.class_belongs_to_module(modul, klass) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/chupakabra_tools/class_helper.rb', line 62

def self.class_belongs_to_module(modul, klass)
    parent_module = klass.parent
    while (true)
        if parent_module == modul
            return true
        end
        parent_module = parent_module.parent
        break if parent_module == Object
    end
    false
end

.class_is_subclass_of(parent, child) ⇒ Object



55
56
57
58
59
60
# File 'lib/chupakabra_tools/class_helper.rb', line 55

def self.class_is_subclass_of(parent, child)
    if parent.nil? || child.nil?
        return false
    end
    child < parent
end

.class_to_i18n(class_name) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/chupakabra_tools/class_helper.rb', line 5

def self.class_to_i18n(class_name)
    unless (class_name && class_name.is_a?(String) || class_name.class.to_s.downcase == "class")
        raise class_name if class_name
        raise "class_name is null"
    end
    class_name.to_s.underscore.gsub('/', '.')
end

.controller_class_to_i18n(controller_class) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/chupakabra_tools/class_helper.rb', line 13

def self.controller_class_to_i18n(controller_class)
    unless controller_class
        raise "supplied controller is null"
    end

    i18n_string = ::ChupakabraTools::ClassHelper.class_to_i18n(controller_class)
    i18n_string.gsub!("_controller", "") if i18n_string
    i18n_string
end

.controller_full_path_underscore(controller) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/chupakabra_tools/class_helper.rb', line 31

def self.controller_full_path_underscore(controller)
    if controller
        underscore_string = controller.to_s.underscore
        underscore_string.gsub!("_controller", "")
        "/#{controller.to_s.underscore.gsub!("_controller", "")}"
    end
end

.controller_underscore(controller) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/chupakabra_tools/class_helper.rb', line 23

def self.controller_underscore(controller)
    if controller && controller.respond_to?(:controller_name)
        underscore_string = controller.controller_name.to_s.underscore
        underscore_string.gsub!("_controller", "")
        underscore_string
    end
end

.entry_has_db_attribute(entry, attribute) ⇒ Object



47
48
49
50
51
52
# File 'lib/chupakabra_tools/class_helper.rb', line 47

def self.entry_has_db_attribute(entry, attribute)
    if entry && attribute

    end
    false
end

.get_class_hierarchy(klass) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/chupakabra_tools/class_helper.rb', line 127

def self.get_class_hierarchy(klass)
    classes_2_inspect = []
    classes_2_inspect.push(klass)
    sc = klass
    while true do
        sc = sc.superclass
        classes_2_inspect.push(sc)
        if sc == Object || sc.nil?
            break
        end
    end
    classes_2_inspect
end

.get_controller_hierarchy(controller) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/chupakabra_tools/class_helper.rb', line 110

def self.get_controller_hierarchy(controller)
    classes_2_inspect = []
    classes_2_inspect.push(controller)
    sc = controller
    while true do
        break if sc.nil?
        sc = sc.superclass
        classes_2_inspect.push(sc)
        if sc == ActionController::Base
            # we reached base application controller
            break
        end
    end
    classes_2_inspect
end

.get_method_all_results(klass, method_name, *method_args) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/chupakabra_tools/class_helper.rb', line 153

def self.get_method_all_results(klass, method_name, *method_args)
    method_results = []
    self.get_class_hierarchy(klass).each do |h_klass|
        if h_klass.respond_to?(method_name)
            res = h_klass.send(method_name, *method_args)
            method_results << res if res
        end
    end
    method_results
end

.get_method_latest_result(klass, method_name, *method_args) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/chupakabra_tools/class_helper.rb', line 142

def self.get_method_latest_result(klass, method_name, *method_args)
    self.get_class_hierarchy(klass).each do |h_klass|
        if h_klass.respond_to?(method_name)
            m_result = h_klass.send(method_name, *method_args)
            return m_result if m_result
        end
    end
    nil
end

.grep(data, template) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/chupakabra_tools/class_helper.rb', line 245

def self.grep(data, template)
    grep_values = []

    if data.is_a?(Array)
        data.each do |dt|
            value = dt.to_s
            if value.index(template)
                grep_values.push(value)
            end
        end
    end
    grep_values
end

.has_action(klass, action) ⇒ Object



39
40
41
# File 'lib/chupakabra_tools/class_helper.rb', line 39

def self.has_action(klass, action)
    klass && action && klass.respond_to?(action)
end

.has_action?(klass, action) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/chupakabra_tools/class_helper.rb', line 43

def self.has_action?(klass, action)
    klass && action && klass.respond_to?(action)
end

.load_all_active_record_modelsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/chupakabra_tools/class_helper.rb', line 75

def self.load_all_active_record_models
    librbfiles = File.join("#{Rails.root}/app/models/", "**", "*.rb")
    results = []

    Dir.glob(librbfiles).each do |file|
        begin
            klass_string = file.gsub("#{Rails.root}/app/models/", "")[0..-4].camelize
            klass = eval(klass_string)
            instance = klass.new

            results.push klass if instance.is_a?(ActiveRecord::Base)
        rescue => ex

        end
    end
    results
end

.property_required?(model, property) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/chupakabra_tools/class_helper.rb', line 93

def self.property_required?(model, property)
    self.attribute_required?(model, property)
end

.test_engine_classesObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/chupakabra_tools/class_helper.rb', line 165

def self.test_engine_classes
    results = []


    Dir.glob(File.join("#{Rails.root}/app/models/", "**", "*.rb")).each do |file|
        begin
            klass_string = file.gsub("#{Rails.root}/app/models/", "")[0..-4].camelize
            klass = eval(klass_string)
            instance = klass.new if klass < ActiveRecord::Base && !klass.abstract_class == true
                #
                #results.push klass if instance.is_a?(ActiveRecord::Base)
        rescue Exception => e
            results << "**************************************************************"
            results << "ERROR: #{e}"
            results << "FILE: #{file}"
            results << e.to_s
            results << "**************************************************************"
        end
    end

    Dir.glob(File.join("#{Rails.root}/app/helpers/", "**", "*.rb")).each do |file|
        begin
            klass_string = file.gsub("#{Rails.root}/app/helpers/", "")[0..-4].camelize
            klass = eval(klass_string)
            instance = klass.new if klass < ActiveRecord::Base && !klass.abstract_class == true
                #
                #results.push klass if instance.is_a?(ActiveRecord::Base)
        rescue Exception => e
            results << "**************************************************************"
            results << "ERROR: #{e}"
            results << "FILE: #{file}"
            results << e.to_s
            results << "**************************************************************"
        end
    end


    Dir.glob(File.join("#{Rails.root}/app/controllers/", "**", "*.rb")).each do |file|
        begin
            klass_string = file.gsub("#{Rails.root}/app/controllers/", "")[0..-4].camelize
            klass = eval(klass_string)
            instance = klass.new if klass < ActiveRecord::Base && !klass.abstract_class == true
                #
                #results.push klass if instance.is_a?(ActiveRecord::Base)
        rescue Exception => e
            results << "**************************************************************"
            results << "ERROR: #{e}"
            results << "FILE: #{file}"
            results << e.to_s
            results << "**************************************************************"
        end
    end


    Rails.logger.info(results)
    results.join("\r\n")
end

.test_gem_classes_for_errorsObject



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/chupakabra_tools/class_helper.rb', line 224

def self.test_gem_classes_for_errors
    results = []
    Dir.glob(File.join("#{Rails.root}/lib/", "**", "*.rb")).each do |file|
        begin
            klass_string = file.gsub("#{Rails.root}/app/models/", "")[0..-4].camelize
            klass = eval(klass_string)
            instance = klass.new if klass < ActiveRecord::Base && !klass.abstract_class == true
                #
                #results.push klass if instance.is_a?(ActiveRecord::Base)
        rescue Exception => e
            results << "**************************************************************"
            results << "ERROR: #{e}"
            results << "FILE: #{file}"
            results << e.to_s
            results << "**************************************************************"
        end
    end
    Rails.logger.info(results)
    results
end