Class: OrientDB::OClassImpl

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

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



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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/orientdb/oclass.rb', line 92

def create(db, name, fields = { })
  name        = name.to_s
  add_cluster = fields.delete :add_cluster
  add_cluster = true if add_cluster.nil?
  use_cluster = fields.delete :use_cluster

  if db.schema.exists_class? name
    klass = db.get_class name
  else
    if use_cluster
      klass = db.schema.create_class name, use_cluster
    elsif add_cluster && !db.storage.cluster_names.include?(name.downcase)
      #debugger
      cluster = db.storage.add_cluster STORAGE_TYPES[:physical], name.downcase, "/tmp/database", 'default', false, {}
      klass   = db.schema.create_class name, cluster
    else
      klass = db.schema.create_class name
    end
  end

  super_klass = fields.delete :super
  super_klass = db.get_class(super_klass.to_s) unless super_klass.is_a?(OrientDB::OClassImpl)
  klass.set_super_class super_klass if super_klass
  db.schema.save

  unless fields.empty?
    fields.each do |property_name, type|
      case type
        when Symbol, Array, OrientDB::OClassImpl
          klass.add property_name, type
        when Hash
          options = type.dup
          type    = options.delete :type
          klass.add property_name, type, options
        else
          raise "Unknown field options [#{type.inspect}]"
      end
    end
    db.schema.save
  end

  klass
end

.type_for(value, schema) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/orientdb/oclass.rb', line 72

def type_for(value, schema)
  value = value.oclass if value.respond_to?(:oclass)
  type = case value
           when OrientDB::SchemaType, OrientDB::OClassImpl
             value
           when String
             if schema.exists_class?(value)
               schema.get_class(value)
             else
               FIELD_TYPES[value.to_sym]
             end
           when Symbol
             FIELD_TYPES[value]
           else
             FIELD_TYPES[value.to_s.to_sym]
         end
  raise "Uknown schema type for [#{value}] (#{value.class.name})" unless type
  type
end

Instance Method Details

#[](property_name) ⇒ Object



47
48
49
50
# File 'lib/orientdb/oclass.rb', line 47

def [](property_name)
  property_name = property_name.to_s
  exists_property(property_name) ? get_property(property_name) : nil
end

#add(property_name, type, options = { }) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/orientdb/oclass.rb', line 9

def add(property_name, type, options = { })
  property_name = property_name.to_s
  if exists_property(property_name)
    puts "We already have that property name [#{property_name}]"
    return false
  end

  type = type.oclass if type.respond_to?(:oclass)
  case type
    when SchemaType
      prop = create_property property_name, type
    when Symbol
      prop = create_property property_name, type_for(type)
    when OClassImpl
      prop = create_property property_name, type_for(:link), type
    when Array
      type, sub_type = type_for(type.first), type_for(type.last)
      prop = create_property property_name, type, sub_type
    else
      raise "ERROR! Unknown type [ #{property_name} | #{type} : #{type.class.name} ]"
  end

  prop.set_min options[:min].to_s unless options[:min].nil?
  prop.set_max options[:max].to_s unless options[:max].nil?
  prop.set_mandatory !!options[:mandatory] unless options[:mandatory].nil?
  prop.set_not_null options[:not_null] unless options[:not_null].nil?
  unless options[:index].nil?
    index_type = options[:index] == true ? INDEX_TYPES[:notunique] : INDEX_TYPES[options[:index]]
    prop.createIndex index_type
  end

  self
end

#add_indexObject



43
44
45
# File 'lib/orientdb/oclass.rb', line 43

def add_index

end

#dbObject



52
53
54
# File 'lib/orientdb/oclass.rb', line 52

def db
  document.database
end

#inspectObject Also known as: to_s



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

def inspect
  props = properties.map { |x| "#{x.name}=#{x.type.name}#{x.is_indexed? ? '(idx)' : ''}" }.join(' ')
  "#<OrientDB::OClassImpl:" + name +
    (getSuperClass ? ' super=' + getSuperClass.name : '') +
    (props.empty? ? '' : ' ' + props) +
    ">"
end

#schemaObject



56
57
58
# File 'lib/orientdb/oclass.rb', line 56

def schema
  db..schema
end

#type_for(value) ⇒ Object



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

def type_for(value)
  self.class.type_for value, schema
end