Class: Spider::Rails

Inherits:
Object show all
Defined in:
lib/spiderfw/utils/rails.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Rails

Returns a new instance of Rails.



7
8
9
10
11
# File 'lib/spiderfw/utils/rails.rb', line 7

def initialize(path, options={})
    @path = path
    @options = options
    @rails_app_name = options[:app_name]
end

Class Method Details

.active_record_models(mod = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/spiderfw/utils/rails.rb', line 58

def active_record_models(mod=nil)
    mod ||= Object
    res = []

    mod.constants.each do |const|
        kl = mod.const_get(const.to_sym)
        if (kl.is_a?(Class))
            res << kl if kl < ::ActiveRecord::Base
        # elsif(kl.is_a?(Module))
        #                        res += active_record_models(kl)
        end
        
    end
    return res
end

Instance Method Details

#define_models(container) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/spiderfw/utils/rails.rb', line 32

def define_models(container)
    start unless started?
    ar_models = []
    
    self.class.active_record_models.each do |mod|
        Spider::Model::create_ar_classes(mod, container)
    end
    done = {}
    # Spider::Model.ar_models.each do |mod|
    #     mod.ar_through_models.each do |m|
    #         next if done[m]
    #         m.define_from_ar
    #         done[m] = true
    #     end
    # end
    
    Spider::Model.ar_models.each do |mod|
        #next if done[mod]
        next unless mod.ar_defined
        mod.rails_app_name = @rails_app_name
        mod.define_from_ar
        #done[mod] = true
    end
end

#startObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/spiderfw/utils/rails.rb', line 13

def start
    return if @started
    Spider::Logger.info("Loading Rails environment at #{@path}")
    require "#{@path}/config/environment.rb"
    ActiveRecord::Base.instance_eval do
        def self.spider_model
            @spider_model
        end
        def self.spider_model=(val)
            @spider_model = val
        end
    end
    @started = true
end

#started?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/spiderfw/utils/rails.rb', line 28

def started?
    @started
end