Module: OrientDB::AR::DocumentMixin::ClassMethods

Defined in:
lib/orientdb-ar/document_mixin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#oclass_nameObject



175
176
177
# File 'lib/orientdb-ar/document_mixin.rb', line 175

def oclass_name
  @oclass_name ||= oclass_name_for name
end

Instance Method Details

#field(name, type, options = { }) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/orientdb-ar/document_mixin.rb', line 179

def field(name, type, options = { })
  name = name.to_sym
  if fields.key? name
    puts "Already defined field [#{name}]"
  else
    fields[name] = { :type => type }.update options
  end
end

#from_orientdb(value) ⇒ Object



203
204
205
# File 'lib/orientdb-ar/document_mixin.rb', line 203

def from_orientdb(value)
  value.respond_to?(:to_orientdb_ar) ? value.to_orientdb_ar : value
end

#new_documentObject



199
200
201
# File 'lib/orientdb-ar/document_mixin.rb', line 199

def new_document
  OrientDB::Document.new connection, oclass_name
end

#oclass_name_for(value) ⇒ Object



171
172
173
# File 'lib/orientdb-ar/document_mixin.rb', line 171

def oclass_name_for(value)
  value.to_s.gsub('::', '__')
end

#schema!Object



188
189
190
191
192
193
194
195
196
197
# File 'lib/orientdb-ar/document_mixin.rb', line 188

def schema!
  fields.each do |field, options|
    begin
      oclass.add field, options[:type], options.except(:type)
    rescue Exception => e
      raise e unless e.message =~ /already exists/
    end
  end
  self
end

#to_orientdb(value) ⇒ Object



207
208
209
210
211
212
213
214
215
# File 'lib/orientdb-ar/document_mixin.rb', line 207

def to_orientdb(value)
  if value.respond_to?(:to_orientdb)
    value.to_orientdb
  elsif value.respond_to?(:jruby_value)
    value.jruby_value
  else
    value
  end
end