Class: Updater::ORM::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/updater/orm/mock.rb

Constant Summary collapse

FINDER =
:find
ID =
:id

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Mock

Returns a new instance of Mock.



95
96
97
98
99
100
# File 'lib/updater/orm/mock.rb', line 95

def initialize(hash = {})
  @id = self.class.index
  hash.each do |k,v| 
    self.send("#{k}=",v)
  end
end

Class Attribute Details

.loggerObject

Returns the value of attribute logger.



10
11
12
# File 'lib/updater/orm/mock.rb', line 10

def logger
  @logger
end

Instance Attribute Details

#finderObject

Returns the value of attribute finder.



93
94
95
# File 'lib/updater/orm/mock.rb', line 93

def finder
  @finder
end

#finder_argsObject

Returns the value of attribute finder_args.



93
94
95
# File 'lib/updater/orm/mock.rb', line 93

def finder_args
  @finder_args
end

#idObject (readonly)

class << self



91
92
93
# File 'lib/updater/orm/mock.rb', line 91

def id
  @id
end

#lock_nameObject

Returns the value of attribute lock_name.



93
94
95
# File 'lib/updater/orm/mock.rb', line 93

def lock_name
  @lock_name
end

#methodObject

Returns the value of attribute method.



93
94
95
# File 'lib/updater/orm/mock.rb', line 93

def method
  @method
end

#method_argsObject

Returns the value of attribute method_args.



93
94
95
# File 'lib/updater/orm/mock.rb', line 93

def method_args
  @method_args
end

#nameObject

Returns the value of attribute name.



93
94
95
# File 'lib/updater/orm/mock.rb', line 93

def name
  @name
end

#persistantObject

Returns the value of attribute persistant.



93
94
95
# File 'lib/updater/orm/mock.rb', line 93

def persistant
  @persistant
end

#targetObject

Returns the value of attribute target.



93
94
95
# File 'lib/updater/orm/mock.rb', line 93

def target
  @target
end

#timeObject

Returns the value of attribute time.



93
94
95
# File 'lib/updater/orm/mock.rb', line 93

def time
  @time
end

Class Method Details

._delayedObject



35
36
37
38
39
# File 'lib/updater/orm/mock.rb', line 35

def _delayed
  storage.values.find_all{|x|
    x.time && x.time > tnow
  }.sort{|a,b| a.time <=> b.time}
end

.clear_allObject



66
67
68
# File 'lib/updater/orm/mock.rb', line 66

def clear_all
  @storage = {}
end

.clear_locks(worker) ⇒ Object



61
62
63
# File 'lib/updater/orm/mock.rb', line 61

def clear_locks(worker)
  storage.values.each{|x| x.lock_name = nil if x.lock_name == worker.name}
end

.create(hash) ⇒ Object



21
22
23
# File 'lib/updater/orm/mock.rb', line 21

def create(hash)
  new(hash).tap {|n| storage[n.id] = n}
end

.currentObject



25
26
27
28
29
# File 'lib/updater/orm/mock.rb', line 25

def current
  storage.values.find_all{|x|
    x.time && x.time <= tnow && !x.lock_name 
  }.sort{|a,b| a.time <=> b.time}
end

.current_loadObject



31
32
33
# File 'lib/updater/orm/mock.rb', line 31

def current_load
  current.length
end

.delayedObject



41
42
43
# File 'lib/updater/orm/mock.rb', line 41

def delayed
  _delayed.length
end

.for(target, finder, finder_args, name = nil) ⇒ Object



74
75
76
77
78
79
# File 'lib/updater/orm/mock.rb', line 74

def for(target, finder, finder_args, name=nil)
  @storage.values.find_all do |x|
    naming = name ? x.name == name : true
    naming && x.finder == finder && x.finder_args == finder_args && x.target == target && !x.lock_name
  end
end

.future(start, finish) ⇒ Object



45
46
47
# File 'lib/updater/orm/mock.rb', line 45

def future(start, finish)
  _delayed.find_all{|x| x.time >= start+tnow && x.time < finish+tnow}
end

.get(id) ⇒ Object



17
18
19
# File 'lib/updater/orm/mock.rb', line 17

def get(id)
  storage[id]
end

.indexObject



12
13
14
15
# File 'lib/updater/orm/mock.rb', line 12

def index
  @index ||= 0
  @index += 1
end

.lock_next(worker) ⇒ Object



55
56
57
58
59
# File 'lib/updater/orm/mock.rb', line 55

def lock_next(worker)
  job = current.first
  job.lock(worker) if job
  job
end

.queue_timeObject



49
50
51
52
53
# File 'lib/updater/orm/mock.rb', line 49

def queue_time
  return 0 unless current.empty?
  return nil if (d = _delayed).empty? #tricky assignment in conditional
  d.first.time - tnow
end

.setup(options) ⇒ Object



70
71
72
# File 'lib/updater/orm/mock.rb', line 70

def setup(options)
  @storage = {} 
end

.storageObject



81
82
83
# File 'lib/updater/orm/mock.rb', line 81

def storage
  @storage ||= {}
end

Instance Method Details

#destroyObject



111
112
113
# File 'lib/updater/orm/mock.rb', line 111

def destroy
  self.class.storage.delete(id)
end

#lock(worker) ⇒ Object



102
103
104
105
# File 'lib/updater/orm/mock.rb', line 102

def lock(worker)
  return false if @lock_name && @lock_name != worker.name
  @lock_name = worker.name
end

#saveObject



107
108
109
# File 'lib/updater/orm/mock.rb', line 107

def save
  self.class.storage[id] = self
end