Class: Ocelot::Dsl

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDsl

Returns a new instance of Dsl.



13
14
15
16
17
18
# File 'lib/ocelot/dsl.rb', line 13

def initialize
  @rules = {}
  @filters = {}
  @seeds = lambda { [] }
  @logger = Logger.new(STDERR)
end

Instance Attribute Details

#filtersObject

Returns the value of attribute filters.



7
8
9
# File 'lib/ocelot/dsl.rb', line 7

def filters
  @filters
end

#rulesObject

Returns the value of attribute rules.



6
7
8
# File 'lib/ocelot/dsl.rb', line 6

def rules
  @rules
end

Instance Method Details

#classes(classes = nil) ⇒ Object



35
36
37
38
# File 'lib/ocelot/dsl.rb', line 35

def classes(classes=nil)
  @classes = classes.split(/\s+/) unless classes.nil?
  @classes
end

#filter(clazz, &p) ⇒ Object



58
59
60
# File 'lib/ocelot/dsl.rb', line 58

def filter(clazz, &p)
  @filters[clazz] = ScriptFilter.new(p)
end

#goObject



65
66
67
68
69
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
# File 'lib/ocelot/dsl.rb', line 65

def go
  Ocelot::logger            = logger
  Ocelot::connection        = Ocelot::Connection.new(source, target)

  unless init.nil?
    Ocelot.connection.use_source = false
    init.call
    Ocelot.connection.use_source = true
  end

  copier = Ocelot::Processor.new
  copier.classes       = classes.collect { |c| eval c }
  copier.seeds         = seeds.call.flatten
  copier.extra_rules   = rules
  copier.extra_filters = filters

  if !@watch
    copier.go
  else
    copier_thread = Thread.new(copier) do
      copier.go
    end
    copier_thread.run
    copier_thread.set_trace_func proc { |*args| copier.watcher.trace(*args) }
    last_warning = nil

    # pole status
    while !copier_thread.status.nil? and copier_thread.status != false
      copier_thread.join(30)
      warning = copier.watcher.ping
      unless warning.nil? or last_warning == warning
        logger.warn warning 
        last_warning = warning
      end
    end
  end
end

#init(&p) ⇒ Object



49
50
51
52
# File 'lib/ocelot/dsl.rb', line 49

def init(&p)
  @init = p unless p.nil?
  @init
end

#log_level(level = nil) ⇒ Object



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

def log_level(level=nil)
  @logger.level = eval "Logger::#{level}" unless level.nil?
  level
end

#loggerObject



40
41
42
# File 'lib/ocelot/dsl.rb', line 40

def logger
  @logger
end

#rule(clazz, &p) ⇒ Object



54
55
56
# File 'lib/ocelot/dsl.rb', line 54

def rule(clazz, &p) 
  @rules[clazz] = ScriptRule.new(p)
end

#seeds(&seeds) ⇒ Object



25
26
27
28
# File 'lib/ocelot/dsl.rb', line 25

def seeds(&seeds)
  @seeds = seeds unless seeds.nil?
  @seeds
end

#source(source = nil) ⇒ Object Also known as: source=



20
21
22
23
# File 'lib/ocelot/dsl.rb', line 20

def source(source=nil)
  @source = source unless source.nil?
  @source
end

#target(target = nil) ⇒ Object Also known as: target=



30
31
32
33
# File 'lib/ocelot/dsl.rb', line 30

def target(target=nil)
  @target = target unless target.nil?
  @target
end

#watch(val) ⇒ Object



9
10
11
# File 'lib/ocelot/dsl.rb', line 9

def watch(val)
  @watch = val
end