Class: Ocelot::Processor

Inherits:
Base
  • Object
show all
Defined in:
lib/ocelot/processor.rb

Defined Under Namespace

Classes: ArrayRule

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#connection, #in?, #logger, #obj_to_s, #remove_callbacks, #safely

Constructor Details

#initializeProcessor

Returns a new instance of Processor.



48
49
50
51
52
53
54
# File 'lib/ocelot/processor.rb', line 48

def initialize
  @extra_rules = {}
  @extra_filters = {}
  @seeds = []
  @unrecognised = Set.new
  @watcher = Watcher.new
end

Instance Attribute Details

#classesObject

Returns the value of attribute classes.



42
43
44
# File 'lib/ocelot/processor.rb', line 42

def classes
  @classes
end

#extra_filtersObject

Returns the value of attribute extra_filters.



44
45
46
# File 'lib/ocelot/processor.rb', line 44

def extra_filters
  @extra_filters
end

#extra_rulesObject

Returns the value of attribute extra_rules.



43
44
45
# File 'lib/ocelot/processor.rb', line 43

def extra_rules
  @extra_rules
end

#savedObject (readonly)

Returns the value of attribute saved.



46
47
48
# File 'lib/ocelot/processor.rb', line 46

def saved
  @saved
end

#seedsObject

Returns the value of attribute seeds.



41
42
43
# File 'lib/ocelot/processor.rb', line 41

def seeds
  @seeds
end

#watcherObject (readonly)

Returns the value of attribute watcher.



45
46
47
# File 'lib/ocelot/processor.rb', line 45

def watcher
  @watcher
end

Instance Method Details

#goObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/ocelot/processor.rb', line 130

def go
  setup_rules_and_filters

  @saved     = Set.new   
  @inspected = Set.new   

  array_rule   = ArrayRule.new

  seeds = @seeds.collect { |o| [o, Rules::INSPECT_DEEP, "script seed"] } 
  Graph::Traverser.run(seeds) do |objs, queue|
    obj, inspect_level, calller = *objs

    unless in? @saved, obj
      inspect_level = process_rule(:obj => obj, :caller => calller)
      @inspected << obj unless inspect_level > Rules::INSPECT_NONE
    end

    unless in? @inspected, obj and inspect_level > Rules::INSPECT_NONE
      obj.class.reflect_on_all_associations.each do |assoc| 
        safely obj do
          connection.use_source = true
          value = obj.send(assoc.name)

          unless value.nil?
 
            # simple has_one (A->B handling), save B and conditionally inspect
            if !assoc.collection?
              unless in? @saved, value
                child_inspect_level = process_rule(:obj => value, :caller => calller) 
                if child_inspect_level > Rules::INSPECT_NONE
                  queue << [value, child_inspect_level, obj_to_s(obj, assoc)]
                end
              end

            # more complicated has many (A-B* and A-mapobj-B*), saving and inspecting each child
            # monkeypatching the array_rule must yield B and whether to inspect it
            # be very very very careful, as it handles saving objects whie yielding results
            elsif inspect_level > Rules::INSPECT_SHALLOW
              array_rule.save(obj, value, assoc, self) do |child, child_inspect_level| 
                @saved << child
                if child_inspect_level > Rules::INSPECT_NONE
                  queue << [child, child_inspect_level,  obj_to_s(obj, assoc)] 
                else
                  @inspected << child 
                end
              end
            end
          end
        end
      end
    end
  end
end

#process_filter(params) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ocelot/processor.rb', line 56

def process_filter(params)
  obj        = params.delete :obj
  collection = params.delete :collection 
  name       = params.delete :name

  result = nil
  filter = @filters[obj.class]

  if !filter.nil?
    watcher.sync do |w|
      w.obj = obj
      w.association = name
      w.rule = filter
    end

    logger.info "Filtering #{obj_to_s(obj)}.#{name} via #{filter.class}"
    result = filter.load(obj, collection, name)
  elsif !@unrecognised.include? obj.class
    logger.error "Unrecognised type #{obj.class}"
    @unrecognised << obj.class
  end
  result || []
end

#process_rule(params) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ocelot/processor.rb', line 80

def process_rule(params)
  obj     = params.delete :obj
  src     = params.delete :src
  target  = params.delete :target
  calller = params.delete :caller 

  rule = @rules[obj.class]

  watcher.sync do |w|
    w.obj = obj
    w.association = nil
    w.rule = rule
  end

  if !rule.nil?
    logger.info "Saving #{obj_to_s(obj)} thru #{rule.class} via #{calller}"
    return rule.save(obj, src, target)
  elsif !@unrecognised.include? obj.class
    logger.warn "Unrecognised type #{obj.class}"
    @unrecognised << obj.class
  end
  Rules::INSPECT_NONE
end

#setup_rules_and_filtersObject



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
# File 'lib/ocelot/processor.rb', line 104

def setup_rules_and_filters
  default_rule   = Rules::DefaultRule.new
  default_filter = Rules::Filter.new

  @rules     = Hash.new
  @filters   = Hash.new

  @classes.each { |c| @rules[c] = default_rule; @filters[c] = default_filter }

  @rules.merge! @extra_rules
  @filters.merge! @extra_filters

  [@rules, @filters].each do |p|
    class << p 
      alias :old_index :[]
      def [](clazz)
        c = clazz
        while c
          return old_index(c) if old_index(c)
          c = c.superclass
        end
      end
    end
  end
end