Class: OrientDB::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/orientdb/document.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &blk) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/orientdb/document.rb', line 31

def method_missing(method_name, *args, &blk)
  return self[method_name] if field?(method_name)

  match = method_name.to_s.match(/(.*?)([?=!]?)$/)
  case match[2]
    when "="
      self[match[1]] =  args.first
    when "?"
      field(match[1]) ? !!self[match[1]] : super
    else
      super
  end
end

Class Method Details

.create(db, klass_name, fields = {}) ⇒ Object



68
69
70
71
72
# File 'lib/orientdb/document.rb', line 68

def create(db, klass_name, fields = {})
  obj = new db, klass_name, fields
  obj.save
  obj
end

.native_newObject



58
# File 'lib/orientdb/document.rb', line 58

alias_method :native_new, :new

.new(db, klass_name, fields = {}) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/orientdb/document.rb', line 60

def new(db, klass_name, fields = {})
  obj = native_new klass_name.to_s
  fields.each do |name, value|
    obj.field name.to_s, value
  end
  obj
end

Instance Method Details

#[](field_name) ⇒ Object



11
12
13
# File 'lib/orientdb/document.rb', line 11

def [](field_name)
  field field_name.to_s
end

#[]=(field_name, value) ⇒ Object



15
16
17
# File 'lib/orientdb/document.rb', line 15

def []=(field_name, value)
  field field_name.to_s, value
end

#field?(name) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/orientdb/document.rb', line 19

def field?(name)
  contains_field(name.to_s) || (schema_class && schema_class.exists_property?(name.to_s))
end

#inspectObject Also known as: to_s



49
50
51
52
# File 'lib/orientdb/document.rb', line 49

def inspect
  props = values.map { |k, v| "#{k}:#{v.inspect}" }.join(' ')
  %{#<OrientDB::Document:#{class_name}:#{rid}#{props.empty? ? '' : ' ' + props}>}
end

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
# File 'lib/orientdb/document.rb', line 23

def respond_to?(method_name)
  return true if field?(method_name.to_s)
  match = method_name.to_s.match(/(.*?)([?=!]?)$/)
  return true if match[2] == '='
  return true if match[2] == '?' && field?(match[1])
  super
end

#ridObject



45
46
47
# File 'lib/orientdb/document.rb', line 45

def rid
  identity.to_s
end

#valuesObject



5
6
7
# File 'lib/orientdb/document.rb', line 5

def values
  field_names.map { |field_name| [field_name, self[field_name]] }
end