Class: Resque::Plugins::DataWarehouse::TransactionRecord
- Inherits:
-
Object
- Object
- Resque::Plugins::DataWarehouse::TransactionRecord
- Defined in:
- lib/resque/plugins/data_warehouse/transaction_record.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#date ⇒ Object
Returns the value of attribute date.
-
#id ⇒ Object
Returns the value of attribute id.
-
#type ⇒ Object
Returns the value of attribute type.
-
#values ⇒ Object
Returns the value of attribute values.
Instance Method Summary collapse
- #empty? ⇒ Boolean
- #execute ⇒ Object
- #fact ⇒ Object
- #from_json(data) ⇒ Object
- #get_lock ⇒ Object
-
#initialize(id, type, date = nil, values = nil, action = 'save') ⇒ TransactionRecord
constructor
A new instance of TransactionRecord.
- #merge(t2) ⇒ Object
- #release_lock ⇒ Object
- #transaction_data ⇒ Object
- #transaction_key ⇒ Object
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
#action ⇒ Object
Returns the value of attribute action.
5 6 7 |
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 5 def action @action end |
#date ⇒ Object
Returns the value of attribute date.
5 6 7 |
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 5 def date @date end |
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 5 def id @id end |
#type ⇒ Object
Returns the value of attribute type.
5 6 7 |
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 5 def type @type end |
#values ⇒ Object
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
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 |
#execute ⇒ Object
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 |
#fact ⇒ Object
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_lock ⇒ Object
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_lock ⇒ Object
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_data ⇒ Object
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_key ⇒ Object
24 25 26 |
# File 'lib/resque/plugins/data_warehouse/transaction_record.rb', line 24 def transaction_key "#{self.type}_#{self.id}" end |