Class: OrientDB::OClassImpl
- Inherits:
-
Object
- Object
- OrientDB::OClassImpl
- Defined in:
- lib/orientdb/oclass.rb
Class Method Summary collapse
Instance Method Summary collapse
- #[](property_name) ⇒ Object
- #add(property_name, type, options = { }) ⇒ Object
- #add_index ⇒ Object
- #db ⇒ Object
- #inspect ⇒ Object (also: #to_s)
- #schema ⇒ Object
- #type_for(value) ⇒ Object
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 = type.dup type = .delete :type klass.add property_name, type, 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, = { }) 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 [:min].to_s unless [:min].nil? prop.set_max [:max].to_s unless [:max].nil? prop.set_mandatory !![:mandatory] unless [:mandatory].nil? prop.set_not_null [:not_null] unless [:not_null].nil? unless [:index].nil? index_type = [:index] == true ? INDEX_TYPES[:notunique] : INDEX_TYPES[[:index]] prop.createIndex index_type end self end |
#add_index ⇒ Object
43 44 45 |
# File 'lib/orientdb/oclass.rb', line 43 def add_index end |
#db ⇒ Object
52 53 54 |
# File 'lib/orientdb/oclass.rb', line 52 def db document.database end |
#inspect ⇒ Object 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 |
#schema ⇒ Object
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 |