Class: CommandPost::Aggregate

Inherits:
Object
  • Object
show all
Defined in:
lib/command_post/event_sourcing/aggregate.rb

Class Method Summary collapse

Class Method Details

.exists?(aggregate_type, aggregate_lookup_value) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/command_post/event_sourcing/aggregate.rb', line 61

def self.exists? aggregate_type, aggregate_lookup_value  
  $DB.fetch("SELECT count(*) as cnt FROM aggregates WHERE aggregate_type = ? and aggregate_lookup_value = ? ", aggregate_type.to_s, aggregate_lookup_value) do |rec|
    return rec[:cnt].to_i > 0
  end
end

.get_aggregate_by_lookup_value(aggregate_type, aggregate_lookup_value) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/command_post/event_sourcing/aggregate.rb', line 69

def self.get_aggregate_by_lookup_value aggregate_type, aggregate_lookup_value 
  hash = Hash.new
  $DB.fetch("SELECT content FROM aggregates WHERE aggregate_type = ? and aggregate_lookup_value = ? ", aggregate_type.to_s, aggregate_lookup_value) do |rec|
    hash = JSON.parse(rec[:content])
  end
  if hash.nil? || hash == {}
    {}
  else
    aggregate_type.load_from_hash( aggregate_type, HashUtil.symbolize_keys(hash))
  end
end

.get_by_aggregate_id(aggregate_type, aggregate_id) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/command_post/event_sourcing/aggregate.rb', line 40

def self.get_by_aggregate_id aggregate_type ,aggregate_id 
  hash = Hash.new
  $DB.fetch("SELECT * FROM aggregates WHERE aggregate_id = ?",  aggregate_id ) do |row|
    hash = JSON.parse(row[:content])
  end
  aggregate_type.load_from_hash( aggregate_type, HashUtil.symbolize_keys(hash))
end

.replace(object, aggregate_lookup_value) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/command_post/event_sourcing/aggregate.rb', line 22

def self.replace object, aggregate_lookup_value


  content = JSON.generate object
  aggregate_id = object[:aggregate_info][:aggregate_id] 
  aggregate_type = object[:aggregate_info][:aggregate_type] 
  version = object[:aggregate_info][:version].to_i


  if (version) == 1
    @@prep_stmt_insert.call(:aggregate_id => aggregate_id, :aggregate_type => aggregate_type.to_s , :content => content, :aggregate_lookup_value => aggregate_lookup_value  ) 
  else
    $DB["UPDATE aggregates set content = ?, aggregate_lookup_value = ? where aggregate_id = ?", content,  aggregate_lookup_value,  aggregate_id ].update
  end
end

.where(aggregate_type) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/command_post/event_sourcing/aggregate.rb', line 50

def self.where(aggregate_type)
  results = Array.new
  $DB.fetch("SELECT * FROM aggregates WHERE aggregate_type = ?", aggregate_type.to_s) do |row|
    hash =  JSON.parse(row[:content])
    results << aggregate_type.load_from_hash( aggregate_type, HashUtil.symbolize_keys(hash))
  end
  results
end