Class: CommandPost::Persistence

Inherits:
Object
  • Object
show all
Includes:
AutoLoad, DataValidation, SchemaValidation
Defined in:
lib/command_post/persistence/persistence.rb

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

Methods included from DataValidation

#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

Methods included from SchemaValidation

validate_allowed_values, validate_auto_load, validate_field_name, validate_keywords, validate_location, validate_lookup, validate_required, validate_schema, validate_type

Constructor Details

#initializePersistence



17
18
19
20
21
22
23
24
# File 'lib/command_post/persistence/persistence.rb', line 17

def initialize 

  @@fields ||= Hash.new
  @aggregate_info_set = false
  @data = Hash.new
  self.class.init_schema self.class.schema
  Command.auto_generate self.class
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(nm, *args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/command_post/persistence/persistence.rb', line 60

def method_missing(nm, *args)

  name = nm.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 
          puts "#{nm} is not a defined field in #{self}"
        end
      end
    end
  else
    nm = name.gsub(/\=/,'').to_sym
    @data[nm] = args.first
  end
end

Class Method Details

.allObject



98
99
100
101
# File 'lib/command_post/persistence/persistence.rb', line 98

def self.all

  Aggregate.where(self)
end

.bypass_auto_loadObject



137
138
139
140
141
# File 'lib/command_post/persistence/persistence.rb', line 137

def self.bypass_auto_load
  @@bypass ||= Hash.new
  @@bypass[self] ||= false 
  @@bypass[self]
end

.bypass_auto_load=(value) ⇒ Object



144
145
146
147
# File 'lib/command_post/persistence/persistence.rb', line 144

def self.bypass_auto_load=(value)
  @@bypass ||= Hash.new
  @@bypass[self]=value 
end

.init_schema(fields) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/command_post/persistence/persistence.rb', line 104

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/command_post/persistence/persistence.rb', line 113

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 #unless self.bypass_auto_load == true
  object.populate_local_persistent_objects
  if (the_class.included_modules.include?(CommandPost::Identity) == true)
    object.set_aggregate_lookup_value
  end
  object
end

.upcase?(field_name) ⇒ Boolean

Raises:

  • (ArgumentError)


150
151
152
153
154
# File 'lib/command_post/persistence/persistence.rb', line 150

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_typeObject



86
87
88
89
# File 'lib/command_post/persistence/persistence.rb', line 86

def aggregate_type
  self.class      
  #@data[:aggregate_info][:aggregate_type]
end

#get_data(name) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/command_post/persistence/persistence.rb', line 41

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

#schema_fieldsObject



27
28
29
30
# File 'lib/command_post/persistence/persistence.rb', line 27

def schema_fields 
  
  @@fields[self.class]
end

#set_data(data_hash) ⇒ Object



33
34
35
36
37
38
# File 'lib/command_post/persistence/persistence.rb', line 33

def set_data data_hash
  @data = data_hash
  if @aggregate_info_set == false 
    @aggregate_info_set = true
  end
end

#to_hObject



92
93
94
95
# File 'lib/command_post/persistence/persistence.rb', line 92

def to_h 

  @data
end