Method: RestCreate#create_properties

Defined in:
lib/rest/create.rb

#create_properties(o_class, all_properties, &b) ⇒ Object

Creates properties

and (if defined in the provided block) associates an index

create_properties(classname or class, properties as hash){index}

The default-case

  create_properties(:my_high_sophisticated_database_class,
		con_id: {type: :integer},
		details: {type: :link, linked_class: 'Contracts'}) do
		  contract_idx: :notunique
		end

A composite index

  create_properties(:my_high_sophisticated_database_class,
		con_id: {type: :integer},
		symbol: {type: :string}) do
	    {name: 'indexname',
			 on: [:con_id, :details]    # default: all specified properties
			 type: :notunique            # default: :unique
	    }
		end


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/rest/create.rb', line 195

def create_properties o_class, all_properties, &b
	logger.progname = 'RestCreate#CreateProperties'
	all_properties_in_a_hash = Hash.new  #WithIndifferentAccess.new
	all_properties.each{|field, args| all_properties_in_a_hash.merge! translate_property_hash(field, args)}
	count, response = 0, nil
#		puts "all_properties_in_a_hash #{all_properties_in_a_hash.to_json}"
	if all_properties_in_a_hash.is_a?(Hash)
		begin
			response = ActiveOrient.db_pool.checkout do | conn |
				 conn["/property/#{ActiveOrient.database}/#{classname(o_class)}"].post all_properties_in_a_hash.to_json
			end
				#				puts response.inspect
				# response.body.to_i returns  response.code, only to_f.to_i returns the correct value
				count= response.body.to_f.to_i if response.code == 201
		rescue RestClient::InternalServerError => e
			logger.progname = 'RestCreate#CreateProperties'
			response = JSON.parse(e.response)['errors'].pop
			error_message = response['content'].split(':').last
			logger.error{"Properties in #{classname(o_class)} were NOT created"}
			logger.error{"The Error was: #{response['content'].split(':').last}"}
			nil
		end
	end
	### index
	if block_given?# && count == all_properties_in_a_hash.size
		index = yield
		if index.is_a?(Hash)
			if index.size == 1
				create_index o_class, name: index.keys.first, on: all_properties_in_a_hash.keys, type: index.values.first
			else
				index_hash =  {type: :unique, on: all_properties_in_a_hash.keys}.merge index
				create_index o_class,  name: index_hash[:name], on: index_hash[:on], type: index_hash[:type]
			end
		end
	end
	count  # return_value
end