Class: Resque::Plugins::DataWarehouse::TransactionRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/resque/plugins/data_warehouse/transaction_record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, type, date = nil, values = nil, action = 'save') ⇒ TransactionRecord

Returns a new instance of TransactionRecord.



7
8
9
10
11
12
13
14
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 7

def initialize(id, type, date = nil, values = nil, action = 'save')
  self.id     = id
  self.type   = type
  self.date   = date
  self.values = values
  self.action = action
  self
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



5
6
7
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 5

def action
  @action
end

#dateObject

Returns the value of attribute date.



5
6
7
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 5

def date
  @date
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 5

def id
  @id
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 5

def type
  @type
end

#valuesObject

Returns the value of attribute values.



5
6
7
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 5

def values
  @values
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 61

def empty?
  self.id.blank? || self.type.blank? || self.date.blank?
end

#executeObject



53
54
55
56
57
58
59
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 53

def execute
  if self.action=='delete'
    self.fact.send("destroy")
  else
    self.fact.send("execute_transaction")
  end
end

#factObject



32
33
34
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 32

def fact
  @fact ||= Fact.find(self.type, self.values)
end

#from_json(data) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 16

def from_json(data)
  data_array  = JSON.parse(data)
  self.date   = Time.parse(data_array[0])
  self.values = data_array[1]
  self.action = data_array[2]
  self
end

#get_lockObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 36

def get_lock
  redis = Resque.redis
  if redis.setnx("#{self.transaction_key}_lock", 1)
    puts "got lock"
    return true
  else
    puts "no lock"
    return false
  end
end

#merge(t2) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 65

def merge(t2)
  if t2.empty?
    return self
  elsif self.empty?
    return t2
  end
  d = (self.date <= t2.date) ? t2.date : self.date
  data = (self.date <= t2.date) ? t2.values : self.values
  action = (self.date <= t2.date) ? t2.action : self.action
  TransactionRecord.new(self.id, self.type, d, data, action)
end

#release_lockObject



47
48
49
50
51
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 47

def release_lock
  redis = Resque.redis
  redis.del("#{self.transaction_key}_lock")
  puts "released lock"
end

#transaction_dataObject



28
29
30
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 28

def transaction_data
  [self.date, self.values, self.action]
end

#transaction_keyObject



24
25
26
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 24

def transaction_key
  "#{self.type}_#{self.id}"
end