Class: CommandPost::Persistence
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from AutoLoad
#auto_load_fields, #local_peristent_fields, #populate_auto_load_fields, #populate_local_persistent_objects, #to_pretty_pp
#accepted_values_supplied_but_they_are_not_all_the_same_type, #allowed_values_declared_but_array_of_values_not_supplied, #data_errors, #data_type_does_not_match_declaration, #empty?, #field_is_array_of_remote_objects_but_array_has_values_other_than_persistence_or_identity, #missing_required_field, #type_is_array_but_keyword___of___not_supplied, #valid?, #value_not_among_the_list_of_allowed_values, #verify_data
validate_allowed_values, validate_auto_load, validate_field_name, validate_keywords, validate_location, validate_lookup, validate_required, validate_schema, validate_type
Constructor Details
Returns a new instance of Persistence.
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/command_post/persistence/persistence.rb', line 16
def initialize
@@fields ||= Hash.new
@@indexes ||= Hash.new
@aggregate_info_set = false
@data = Hash.new
self.class.init_schema self.class.schema
self.class.init_indexes self.class.indexes
Command.auto_generate self.class
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(nm, *args) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/command_post/persistence/persistence.rb', line 126
def method_missing(nm, *args)
name = nm.to_s
error_msg = "SCHEMA ERROR: #{name} is not a defined attribute of or index on '#{self.class.to_s}'"
if name.end_with?('=') == false
if @data.keys.include? nm
get_data nm
else
if schema_fields.keys.include? nm
return nil
else
begin
super
rescue Exception => e
raise error_msg
end
end
end
else
nm = name.gsub(/\=/,'').to_sym
raise error_msg unless schema_fields.keys.include?(nm)
@data[nm] = args.first
end
end
|
Class Method Details
.all ⇒ Object
165
166
167
168
|
# File 'lib/command_post/persistence/persistence.rb', line 165
def self.all
Aggregate.where(self)
end
|
.bypass_auto_load ⇒ Object
214
215
216
217
218
|
# File 'lib/command_post/persistence/persistence.rb', line 214
def self.bypass_auto_load
@@bypass ||= Hash.new
@@bypass[self] ||= false
@@bypass[self]
end
|
.bypass_auto_load=(value) ⇒ Object
221
222
223
224
|
# File 'lib/command_post/persistence/persistence.rb', line 221
def self.bypass_auto_load=(value)
@@bypass ||= Hash.new
@@bypass[self]=value
end
|
.compute_index_column_name(field) ⇒ Object
89
90
91
92
93
|
# File 'lib/command_post/persistence/persistence.rb', line 89
def self.compute_index_column_name(field)
return 'INDEX_VALUE_INTEGER' if field.is_a? Fixnum
return 'INDEX_VALUE_DECIMAL' if field.is_a? BigDecimal
'INDEX_VALUE_TEXT'
end
|
.get_ids_for_in(index_name, *args) ⇒ Object
96
97
98
99
|
# File 'lib/command_post/persistence/persistence.rb', line 96
def self.get_ids_for_in index_name, *args
name = "#{self.to_s}.#{index_name.to_s.sub(/_in$/,'')}"
$DB.fetch(get_sql_for_in name, args.last).collect { |row| row[:aggregate_id] }
end
|
.get_ids_for_is(index_name, value) ⇒ Object
101
102
103
104
|
# File 'lib/command_post/persistence/persistence.rb', line 101
def self.get_ids_for_is index_name, value
name = "#{self.to_s}.#{index_name.to_s.sub(/_is$/,'')}"
$DB.fetch(get_sql_for_is(name,value),value.to_s).collect { |row| row[:aggregate_id] }
end
|
.get_sql_for_in(name, values) ⇒ Object
78
79
80
|
# File 'lib/command_post/persistence/persistence.rb', line 78
def self.get_sql_for_in name, values
"SELECT aggregate_id FROM aggregate_indexes WHERE index_field = '#{name}' AND #{self.compute_index_column_name(values.first)} in (#{self.listify(values)}) "
end
|
.get_sql_for_is(name, value) ⇒ Object
83
84
85
|
# File 'lib/command_post/persistence/persistence.rb', line 83
def self.get_sql_for_is name , value
"SELECT aggregate_id FROM aggregate_indexes WHERE index_field = '#{name}' AND #{self.compute_index_column_name(value)} = ? "
end
|
.init_indexes(index_fields) ⇒ Object
179
180
181
182
183
184
185
186
|
# File 'lib/command_post/persistence/persistence.rb', line 179
def self.init_indexes index_fields
@@indexes ||= Hash.new
@@indexes[self] ||= index_fields
end
|
.init_schema(fields) ⇒ Object
171
172
173
174
175
176
177
|
# File 'lib/command_post/persistence/persistence.rb', line 171
def self.init_schema fields
schema_error_messages = SchemaValidation.validate_schema(fields)
if schema_error_messages.length > 0
raise ArgumentError, "The schema for #{self} had the following error(s): #{pp schema_error_messages}"
end
@@fields[self] ||= fields
end
|
.listify(values) ⇒ Object
72
73
74
75
|
# File 'lib/command_post/persistence/persistence.rb', line 72
def self.listify(values)
return values.collect { |x| "'#{x}'"}.join(',') if values.first.is_a? String
values.collect { |x| "#{x}" }.join(',')
end
|
.load_from_hash(the_class, string_hash) ⇒ Object
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
# File 'lib/command_post/persistence/persistence.rb', line 190
def self.load_from_hash the_class, string_hash
data_hash = HashUtil.symbolize_keys(string_hash)
if (data_hash.keys.include?(:aggregate_info) == false) && (the_class.included_modules.include?(CommandPost::Identity) == true)
data_hash[:aggregate_info] = Hash.new
data_hash[:aggregate_info][:aggregate_type] = the_class.to_s
data_hash[:aggregate_info][:version] = 1
data_hash[:aggregate_info][:aggregate_id] = SequenceGenerator.aggregate_id
end
object = the_class.new
object.set_data data_hash
object.populate_auto_load_fields
object.populate_local_persistent_objects
object
end
|
.method_missing(nm, *args, &block) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/command_post/persistence/persistence.rb', line 107
def self.method_missing nm, *args , &block
name = nm.to_s
if (name.end_with?('_in') && self.indexes.include?(name.gsub(/_in/,'').to_sym))
ids = self.get_ids_for_in nm, args
return ids
elsif (name.end_with?('_is') && self.indexes.include?(name.gsub(/_is/,'').to_sym))
ids = self.get_ids_for_is nm, args.last
return ids
else
begin
super
rescue Exception => e
puts "DEBUG: PRINTING BACKTRACE #{'=' * 80}"
puts e.backtrace
end
end
end
|
.stringify_values(values) ⇒ Object
66
67
68
69
70
|
# File 'lib/command_post/persistence/persistence.rb', line 66
def self.stringify_values values
values.collect{|x| "'#{x}'"}.join(',')
end
|
.upcase?(field_name) ⇒ Boolean
227
228
229
230
231
|
# File 'lib/command_post/persistence/persistence.rb', line 227
def self.upcase? field_name
raise ArgumentError ,"field not found " if (schema.keys.include?(field_name) == false)
field_info = schema[field_name]
field_info.keys.include?(:upcase) ? field_info[:upcase] : false
end
|
Instance Method Details
#aggregate_type ⇒ Object
153
154
155
156
|
# File 'lib/command_post/persistence/persistence.rb', line 153
def aggregate_type
self.class
end
|
#get_data(name) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/command_post/persistence/persistence.rb', line 47
def get_data name
if schema_fields[name][:location] == :local
if schema_fields[name][:type] == DateTime
return DateHelper.parse_date_time(@data[name])
elsif schema_fields[name][:type] == Time
DateHelper.parse_time(@data[name])
else
return @data[name]
end
else
if @data[name].class == Hash && @data[name].keys == [:aggregate_type,:aggregate_id]
Aggregate.get_by_aggregate_id(schema_fields[name][:type], @data[name][:aggregate_id])
else
@data[name]
end
end
end
|
#index_fields ⇒ Object
33
34
35
|
# File 'lib/command_post/persistence/persistence.rb', line 33
def index_fields
@@indexes[self.class]
end
|
#schema_fields ⇒ Object
28
29
30
31
|
# File 'lib/command_post/persistence/persistence.rb', line 28
def schema_fields
@@fields[self.class]
end
|
#set_data(data_hash) ⇒ Object
39
40
41
42
43
44
|
# File 'lib/command_post/persistence/persistence.rb', line 39
def set_data data_hash
@data = data_hash
if @aggregate_info_set == false
@aggregate_info_set = true
end
end
|
#to_h ⇒ Object
159
160
161
162
|
# File 'lib/command_post/persistence/persistence.rb', line 159
def to_h
@data
end
|