Class: CommandPost::Aggregate
- Inherits:
-
Object
- Object
- CommandPost::Aggregate
- Defined in:
- lib/command_post/event_sourcing/aggregate.rb
Class Method Summary collapse
- .exists?(aggregate_type, aggregate_lookup_value) ⇒ Boolean
- .get_aggregate_by_lookup_value(aggregate_type, aggregate_lookup_value) ⇒ Object
- .get_by_aggregate_id(aggregate_type, aggregate_id) ⇒ Object
- .replace(object, aggregate_lookup_value) ⇒ Object
- .where(aggregate_type) ⇒ Object
Instance Method Summary collapse
Class Method Details
.exists?(aggregate_type, aggregate_lookup_value) ⇒ Boolean
71 72 73 74 75 |
# File 'lib/command_post/event_sourcing/aggregate.rb', line 71 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
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/command_post/event_sourcing/aggregate.rb', line 79 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
50 51 52 53 54 55 56 |
# File 'lib/command_post/event_sourcing/aggregate.rb', line 50 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
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/command_post/event_sourcing/aggregate.rb', line 20 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.to_i, :aggregate_type => aggregate_type , :content => content, :aggregate_lookup_value => aggregate_lookup_value ) object = Aggregate.get_by_aggregate_id Object.const_get(aggregate_type), aggregate_id object.index_fields.each do |field| index_value = object.send field index_field = "#{object.class.to_s}.#{field.to_s}" $DB["INSERT INTO aggregate_indexes (aggregate_id , #{Persistence.compute_index_column_name(index_value)}, index_field ) values (?, ?, ?) ", aggregate_id, index_value, index_field ].insert end else $DB["UPDATE aggregates set content = ?, aggregate_lookup_value = ? where aggregate_id = ?", content, aggregate_lookup_value, aggregate_id ].update object = Aggregate.get_by_aggregate_id Object.const_get(aggregate_type), aggregate_id @@prep_stmt_insert.call(:aggregate_id => aggregate_id.to_i, :aggregate_type => aggregate_type , :content => content, :aggregate_lookup_value => aggregate_lookup_value ) object.index_fields.each do |field| index_value = object.send field update_index object, index_value, field end end end |
.where(aggregate_type) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/command_post/event_sourcing/aggregate.rb', line 60 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 |
Instance Method Details
#update_index(object, index_value, field) ⇒ Object
15 16 17 18 |
# File 'lib/command_post/event_sourcing/aggregate.rb', line 15 def update_index object, index_value , field index_field = "#{object.class.to_s}.#{field.to_s}" $DB["UPDATE aggregate_indexes set #{Persistence.compute_index_column_name(field) } = ? where aggregate_id = ? and index_field = ?", index_value, aggregate_id.to_i, index_field ].update end |