Class: Hws::Transactions::Models::TransactionGroup

Inherits:
Base
  • Object
show all
Defined in:
lib/hws-transactions/models/transaction_group.rb

Constant Summary collapse

DEFAULT_LIMIT =
10

Instance Method Summary collapse

Methods inherited from Base

#set_uuid

Instance Method Details

#add_entry(value, txn_time, tags = {}) ⇒ Object

tags: hash of the form mentioned below.

'mutable_tags': {mutable_tag_key1: m_tag_val1, mutable_tag_key2: m_tag_val2,
'immutable_tags': i_tag_val1, immutable_tag_key2: i_tag_val2

}



17
18
19
20
21
22
23
24
25
26
# File 'lib/hws-transactions/models/transaction_group.rb', line 17

def add_entry(value, txn_time, tags = {})
  tags = validate_and_sanitize_tags(tags)

  self.transaction_entries.create(
    value: value,
    txn_time: txn_time,
    mutable_tags: tags['mutable_tags'],
    immutable_tags: tags['immutable_tags']
  )
end

#immutable_tagsObject



72
73
74
75
# File 'lib/hws-transactions/models/transaction_group.rb', line 72

def immutable_tags
  @i_tags ||= self.tags['immutable_tags'].to_set
  @i_tags
end

#list_entries(filters = {}, page_context = {}) ⇒ Object

filters struture:

'col1' => 'val1',
'col2' => 'val2',
'mut1' => {
  'jsonf1' => 'val1',
  'jsonf2' => {'nested_jsonf3' => 'val2'
}

} pagination_context structure: {

'last_entry': '',
'page_size': 10

}



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hws-transactions/models/transaction_group.rb', line 54

def list_entries(filters = {}, page_context = {})
  filtered_query = if filters.empty?
                     self.transaction_entries
                   else
                     Hws::Helpers::Query.construct_query(self.transaction_entries, filters)
                   end

  unless page_context['last_entry'].nil?
    filtered_query = filtered_query.where('id > ?', page_context['last_entry'])
  end
  filtered_query.order('id').limit(page_context['page_size'] || DEFAULT_LIMIT)
end

#mutable_tagsObject



67
68
69
70
# File 'lib/hws-transactions/models/transaction_group.rb', line 67

def mutable_tags
  @m_tags ||= self.tags['mutable_tags'].to_set
  @m_tags
end

#update_entry(entry_id, mutable_tags) ⇒ Object

only mutable tags are allowed to be updated TODO: Optimistic locking?



30
31
32
33
34
35
36
37
38
# File 'lib/hws-transactions/models/transaction_group.rb', line 30

def update_entry(entry_id, mutable_tags)
  entry = self.transaction_entries.find(entry_id)

  unless mutable_tags.keys.all? { |key| self.mutable_tags.include?(key) }
    raise 'Invalid mutable tags present in update_entry request.'
  end

  entry.update!(mutable_tags: entry.mutable_tags.merge(mutable_tags))
end