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.
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/command_post/persistence/persistence.rb', line 20
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/command_post/persistence/persistence.rb', line 114
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
153
154
155
156
|
# File 'lib/command_post/persistence/persistence.rb', line 153
def self.all
Aggregate.where(self)
end
|
.bypass_auto_load ⇒ Object
202
203
204
205
206
|
# File 'lib/command_post/persistence/persistence.rb', line 202
def self.bypass_auto_load
@@bypass ||= Hash.new
@@bypass[self] ||= false
@@bypass[self]
end
|
.bypass_auto_load=(value) ⇒ Object
209
210
211
212
|
# File 'lib/command_post/persistence/persistence.rb', line 209
def self.bypass_auto_load=(value)
@@bypass ||= Hash.new
@@bypass[self]=value
end
|
.get_ids_for_index(index_name, *args) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/command_post/persistence/persistence.rb', line 82
def self.get_ids_for_index index_name, *args
values = args[0][0]
name = "#{self.to_s}.#{index_name.to_s.sub(/_in$/,'')}"
sql = get_index_sql name, values
results = Array.new
$DB.fetch(sql) do |row|
results << row[:aggregate_id]
end
results
end
|
.get_index_sql(name, values) ⇒ Object
76
77
78
79
|
# File 'lib/command_post/persistence/persistence.rb', line 76
def self.get_index_sql name, values
"SELECT aggregate_id FROM aggregate_indexes WHERE index_field = '#{name}' AND index_value in (#{self.stringify_values(values)}) "
end
|
.init_indexes(index_fields) ⇒ Object
167
168
169
170
171
172
173
174
|
# File 'lib/command_post/persistence/persistence.rb', line 167
def self.init_indexes index_fields
@@indexes ||= Hash.new
@@indexes[self] ||= index_fields
end
|
.init_schema(fields) ⇒ Object
159
160
161
162
163
164
165
|
# File 'lib/command_post/persistence/persistence.rb', line 159
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
|
.load_from_hash(the_class, string_hash) ⇒ Object
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
# File 'lib/command_post/persistence/persistence.rb', line 178
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/command_post/persistence/persistence.rb', line 97
def self.method_missing nm, *args , &block
name = nm.to_s
search_index = name.gsub(/_in/,'').to_sym
if (name.end_with?('_in') && self.indexes.include?(search_index))
ids = self.get_ids_for_index nm, args
return ids
else
begin
super
rescue Exception => e
puts "DEBUG: PRINTING BACKTRACE #{'=' * 80}"
puts e.backtrace
end
end
end
|
.stringify_values(values) ⇒ Object
70
71
72
73
74
|
# File 'lib/command_post/persistence/persistence.rb', line 70
def self.stringify_values values
values.collect{|x| "'#{x}'"}.join(',')
end
|
.upcase?(field_name) ⇒ Boolean
215
216
217
218
219
|
# File 'lib/command_post/persistence/persistence.rb', line 215
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
141
142
143
144
|
# File 'lib/command_post/persistence/persistence.rb', line 141
def aggregate_type
self.class
end
|
#get_data(name) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/command_post/persistence/persistence.rb', line 51
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
37
38
39
|
# File 'lib/command_post/persistence/persistence.rb', line 37
def index_fields
@@indexes[self.class]
end
|
#schema_fields ⇒ Object
32
33
34
35
|
# File 'lib/command_post/persistence/persistence.rb', line 32
def schema_fields
@@fields[self.class]
end
|
#set_data(data_hash) ⇒ Object
43
44
45
46
47
48
|
# File 'lib/command_post/persistence/persistence.rb', line 43
def set_data data_hash
@data = data_hash
if @aggregate_info_set == false
@aggregate_info_set = true
end
end
|
#to_h ⇒ Object
147
148
149
150
|
# File 'lib/command_post/persistence/persistence.rb', line 147
def to_h
@data
end
|