Class: ObjectType

Inherits:
Object
  • Object
show all
Defined in:
lib/ObjectModel/Types/ObjectType.rb

Class Method Summary collapse

Class Method Details

.delete(e, m, storage) ⇒ Object



48
49
50
# File 'lib/ObjectModel/Types/ObjectType.rb', line 48

def delete e, m, storage          
  storage[:value_objects].filter(:entity_id => e.entity_id, :name => m.name.to_s).delete
end

.initial_value(m, e) ⇒ Object



4
5
6
7
# File 'lib/ObjectModel/Types/ObjectType.rb', line 4

def initial_value m, e
  nil
  #      e.instance_variable_set m.ivname, nil
end

.initialize_copy(m, e, c) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ObjectModel/Types/ObjectType.rb', line 19

def initialize_copy m, e, c     
  value = e.instance_variable_get(m.ivname)
  value_copy = if value == nil
    nil
  elsif value.respond_to? :copy
    value.copy
  else
    copy_object value
  end
  c[m.ivname] = value_copy
end

.initialize_storage(db) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/ObjectModel/Types/ObjectType.rb', line 9

def initialize_storage db     
  db.create_table :value_objects do
    column :entity_id, :text
    column :name, :text 
    column :yaml, :text       
    column :class, :text
    #       index :entity_id
  end
end

.load(m, e, storage) ⇒ Object

Raises:



52
53
54
55
56
57
58
59
60
# File 'lib/ObjectModel/Types/ObjectType.rb', line 52

def load m, e, storage  
  row = storage[:value_objects][:entity_id => e.entity_id, :name => m.name.to_s]
  raise LoadError unless row
  
  value = load_object row[:yaml], row[:class] #YAML.load row[:yaml] # YAML doesn't raise ConstMissing, so we can't just load.
  
  freeze_all_tree value
  e.instance_variable_set m.ivname, value
end

.persist(c, entity_id, m, storage) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/ObjectModel/Types/ObjectType.rb', line 37

def persist c, entity_id, m, storage      
  storage[:value_objects].filter(:entity_id => entity_id, :name => m.name.to_s).delete
  value, klass = dump_object c[m.ivname]     
  storage[:value_objects].insert(
                                :entity_id => entity_id, 
                                :name => m.name.to_s,
                                :yaml => value,
                                :class => klass
  )      
end


62
63
64
65
66
# File 'lib/ObjectModel/Types/ObjectType.rb', line 62

def print_storage db, name
  return unless name == nil or name == :value_objects
  puts "\nValueObjects: size = #{db[:value_objects].size}"
  #      db[:value_objects].print
end

.validate_type(value) ⇒ Object



68
69
70
# File 'lib/ObjectModel/Types/ObjectType.rb', line 68

def validate_type value
  !(value.is_a?(Entity) or value.is_a?(Proc))
end

.write_back(c, e, m) ⇒ Object



31
32
33
34
35
# File 'lib/ObjectModel/Types/ObjectType.rb', line 31

def write_back c, e, m        
  value = c[m.ivname]
  freeze_all_tree value
  e.instance_variable_set m.ivname, value
end

.yaml_load(data) ⇒ Object

Becouse YAML don’t raise ConstMissing error we need to preload all Classes from YAML data.



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ObjectModel/Types/ObjectType.rb', line 73

def yaml_load data
  begin
    doc = YAML.parse data
    load_classes_for doc
    o = doc.transform
    
    return o
  rescue Exception => e
    warn e
    raise e
  end
end