Class: ODDB::Model

Inherits:
Object
  • Object
show all
Includes:
ODBA::Persistable, Yaml
Defined in:
lib/oddb/model.rb,
lib/oddb/model.rb,
lib/oddb/export/yaml.rb,
lib/oddb/persistence/og/model.rb,
lib/oddb/persistence/odba/model.rb,
lib/oddb/persistence/odba/export.rb

Overview

forward definitions (circular dependency Model <-> M10lDocument)

Defined Under Namespace

Classes: Predicate

Constant Summary

Constants included from OddbUri

OddbUri::YAML_URI

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ODBA::Persistable

#to_yaml_properties

Methods included from Yaml

append_features, #to_yaml_properties

Methods included from OddbUri

#to_yaml, #to_yaml_map, #to_yaml_type

Class Method Details

._serializablesObject



51
52
53
54
55
56
57
# File 'lib/oddb/persistence/odba/model.rb', line 51

def _serializables
  if((kls = ancestors.at(1)) && kls.respond_to?(:serializables))
    kls.serializables.dup
  else
    []
  end
end

.belongs_to(groupname, *predicates) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/oddb/model.rb', line 38

def belongs_to(groupname, *predicates)
  attr_reader groupname
  varname = :"@#{groupname}"
  connections.push(varname)
  selfname = singular
  define_method("#{groupname}=") { |group|
    old = instance_variable_get(varname)
    if(old != group)
      if(old)
        old.send("remove_#{selfname}", self)
        old.save
      end
      if(group)
        group.send("add_#{selfname}", self)
        group.save
      end
    end
    instance_variable_set(varname, group)
  }
  predicates.each { |predicate|
    if(predicate.action == :method_missing)
      predicate.delegators.each { |key|
        define_method(key) { 
          if(group = instance_variable_get(varname))
            group.send(key)
          end
        }
      }
    else
      predicate.delegators.push(groupname)
      self.predicates.push(predicate)
    end
  }
end

.connectionsObject



72
73
74
# File 'lib/oddb/model.rb', line 72

def connections
  @connections ||= []
end

.connector(key) ⇒ Object



75
76
77
# File 'lib/oddb/model.rb', line 75

def connector(key)
  connectors.push :"@#{key}"
end

.connectorsObject



78
79
80
# File 'lib/oddb/model.rb', line 78

def connectors
  @connectors ||= []
end

.delegates(*delegators) ⇒ Object



81
82
83
# File 'lib/oddb/model.rb', line 81

def delegates(*delegators)
  Predicate.new(:method_missing, :delegate, *delegators)
end

.find_by_uid(uid) ⇒ Object



44
45
46
47
# File 'lib/oddb/persistence/odba/model.rb', line 44

def find_by_uid(uid)
  obj = ODBA.cache.fetch(uid)
  obj if(obj.class == self)
end

.has_many(plural, *predicates) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/oddb/model.rb', line 84

def has_many(plural, *predicates)
  varname = :"@#{plural}"
  define_method(plural) {
    instance_variable_get(varname) or begin
      instance_variable_set(varname, Array.new)
    end
  }
  define_method("add_#{plural.to_s.singular}") { |inst|
    container = self.send(plural)
    unless(container.any? { |other| inst.eql? other })
      container.push(inst) 
    end
    inst
  }
  define_method("remove_#{plural.to_s.singular}") { |inst|
    self.send(plural).delete_if { |other| inst.eql? other }
  }
  connectors.push(varname)
  predicates.each { |predicate|
    if(predicate.type == :delegate)
      predicate.delegators.each { |key|
        define_method(key) {
          self.send(plural).collect { |inst|
            inst.send(key)
          }.flatten
        }
      }
    else
      predicate.delegators.push(plural)
      self.predicates.push(predicate)
    end
  }
end

.is_codedObject



126
127
128
129
130
131
132
# File 'lib/oddb/model.rb', line 126

def is_coded
  has_many :codes
  define_method(:code) { |*args|
    type, country = *args
    codes.find { |code| code.is_for?(type, country || ODDB.config.country) }
  }
end

.m10l_document(key) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/oddb/model.rb', line 133

def m10l_document(key)
  varname = :"@#{key}"
  define_method(key) {
    instance_variable_get(varname) or begin
      instance_variable_set(varname, Util::M10lDocument.new)
    end
  }
  connectors.push varname
end

.multilingual(key) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/oddb/model.rb', line 142

def multilingual(key)
  define_method(key) {
    instance_variable_get(:"@#{key}") or begin
      instance_variable_set(:"@#{key}", Util::Multilingual.new)
    end
  }
  define_method(:to_s) { 
    self.send(key).to_s
  }
end

.on_delete(action, *delegators) ⇒ Object



117
118
119
# File 'lib/oddb/model.rb', line 117

def on_delete(action, *delegators)
  Predicate.new(:delete, action, *delegators)
end

.on_save(action, *delegators) ⇒ Object



120
121
122
# File 'lib/oddb/model.rb', line 120

def on_save(action, *delegators)
  Predicate.new(:save, action, *delegators)
end

.predicatesObject



123
124
125
# File 'lib/oddb/model.rb', line 123

def predicates
  @predicates ||= []
end

.serializablesObject



48
49
50
# File 'lib/oddb/persistence/odba/model.rb', line 48

def serializables
  @serializables ||= _serializables
end

.serialize(*keys) ⇒ Object



155
156
# File 'lib/oddb/model.rb', line 155

def serialize(key)
end

.singularObject



152
153
154
# File 'lib/oddb/model.rb', line 152

def singular
  basename.gsub(/([a-z])([A-Z])/, '\1_\2').downcase
end

Instance Method Details

#__odba_delete__Object



10
11
12
13
14
15
# File 'lib/oddb/persistence/odba/model.rb', line 10

def delete
  self.class.predicates.each { |predicate|
    predicate.execute(:delete, self)
  }
  self
end

#__odba_save__Object



25
26
27
28
29
30
# File 'lib/oddb/persistence/odba/model.rb', line 25

def save
  self.class.predicates.each { |predicate|
    predicate.execute(:save, self)
  }
  self
end

#data_origin(key) ⇒ Object



158
159
160
# File 'lib/oddb/model.rb', line 158

def data_origin(key)
  data_origins[key]
end

#data_originsObject



161
162
163
# File 'lib/oddb/model.rb', line 161

def data_origins
  @data_origins ||= {}
end

#deleteObject



164
165
166
167
168
169
# File 'lib/oddb/model.rb', line 164

def delete
  self.class.predicates.each { |predicate|
    predicate.execute(:delete, self)
  }
  self
end

#odba_serializablesObject



21
22
23
# File 'lib/oddb/persistence/odba/model.rb', line 21

def odba_serializables
  super.concat self.class.serializables
end

#oidObject



22
23
24
# File 'lib/oddb/persistence/odba/export.rb', line 22

def oid
  @odba_id
end

#saveObject



170
171
172
173
174
175
# File 'lib/oddb/model.rb', line 170

def save
  self.class.predicates.each { |predicate|
    predicate.execute(:save, self)
  }
  self
end

#saved?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/oddb/persistence/odba/model.rb', line 37

def saved?
  !odba_unsaved?
end