lib/arcade/base.rb in arcadedb-0.3.3 vs lib/arcade/base.rb in arcadedb-0.4
- old
+ new
@@ -3,11 +3,11 @@
extend Arcade::Support::Sql
# schema schema.strict # -- throws an error if specified keys are missing
transform_keys{ |x| x[0] == '@' ? x[1..-1].to_sym : x.to_sym }
# Types::Rid --> only accept #000:000, raises an Error, if rid is not present
- attribute :rid, Types::Rid
+ attribute :rid?, Types::Rid
# maybe there are edges ## removed in favour of instance methods
# attribute :in?, Types::Nominal::Any
# attribute :out?, Types::Nominal::Any
# any not defined property goes to values
attribute :values?, Types::Nominal::Hash
@@ -39,16 +39,11 @@
begin
loop do
if the_class.respond_to?(:demodulize)
if [ 'Document','Vertex', 'Edge'].include?(the_class.demodulize)
if the_class == superclass # no inheritance
- ## we have to use demodulize as the_class actually is Arcade::Vertex, ...
- unless parent_present[ to_s.snake_case ]
db.create_type the_class.demodulize, to_s.snake_case
- else
- db.logger.warn "Type #{ to_s.snake_case } is present, process skipped"
- end
else
if superclass.is_a? Class # maybe its a module.
extended = superclass.to_s.snake_case
else
extended = superclass.superclass.to_s.snake_case
@@ -73,11 +68,11 @@
next if the_command == ''
# db.logger.info "Custom Setup:: #{the_command}"
db.execute { the_command }
end unless custom_setup.nil?
- rescue Arcade::RollbackError => e
+ rescue RollbackError => e
db.logger.warn e
rescue RuntimeError => e
db.logger.warn e
end
end
@@ -132,14 +127,14 @@
#
# returns the model dataset
# ( depreciated )
def create **attributes
- Api.begin_transaction db.database
+ s = Api.begin_transaction db.database
attributes.merge!( created: DateTime.now ) if timestamps
record = insert **attributes
- Api.commit db.database
+ Api.commit db.database, s
record
rescue QueryError => e
db.logger.error "Dataset NOT created"
db.logger.error "Provided Attributes: #{ attributes.inspect }"
# Api.rollback db.database ---> raises "transactgion not begun"
@@ -220,12 +215,11 @@
end
# Finds the first matching record providing the parameters of a `where` query
# Strategie.find symbol: 'Still'
# is equivalent to
- # Strategie.all.find{|y| y.symbol == 'Still'
- # }
+ # Strategie.all.find{|y| y.symbol == 'Still' }
def find **args
f= where(**args).first
f= where( "#{ args.keys.first } like #{ args.values.first.to_or }" ).first if f.nil? || f.empty?
f
end
@@ -276,18 +270,18 @@
else
{ where: where_statement }
end
result= query( **( { kind: :upsert }.merge statement ) ).execute do | answer|
z= answer[:"$current"] &.allocate_model(false) # do not autoload modelfiles
- raise Arcade::LoadError "Upsert failed" unless z.is_a? Arcade::Base
+ raise LoadError "Upsert failed" unless z.is_a? Base
z # return record
end
end
def query **args
- Arcade::Query.new( **{ from: self }.merge(args) )
+ Query.new( **{ from: self }.merge(args) )
end
# immutable support
# to make a database type immutable add
# `not_permitted :update, :upsert, :delete`
@@ -330,11 +324,11 @@
return values.fetch(method)
end
end
def query **args
- Arcade::Query.new( **{ from: rid }.merge(args) )
+ Query.new( **{ from: rid }.merge(args) )
end
# to JSON controlls the serialisation of Arcade::Base Objects for the HTTP-JSON API
#
# ensure, that only the rid is transmitted to the database
@@ -362,30 +356,35 @@
def to_human
"<#{ self.class.to_s.snake_case }" + rid? ? "[#{ rid }]: " : " " + invariant_attributes.map do |attr, value|
v= case value
- when Arcade::Base
+ when Base
"< #{ self.class.to_s.snake_case }: #{ value.rid } >"
when Array
value.map{|x| x.to_s}
else
value.from_db
end
"%s : %s" % [ attr, v] unless v.nil?
end.compact.sort.join(', ') + ">".gsub('"' , ' ')
end
- alias to_s to_human
+ # configure irb-output to to_human for all Arcade::Base-Objects
+ #
+ def inspect
+ to_human
+ end
+
def to_html # iruby
_modul, _class = self.class.to_s.split "::"
the_class = _modul == 'Arcade' ? _class : self.class.to_s
IRuby.display IRuby.html "<b style=\"color: #50953DFF\"><#{ the_class}</b>"
+ rid? ? "[#{ rid }]: " : " " + invariant_attributes.map do |attr, value|
v= case value
- when Arcade::Base
+ when Base
"< #{ self.class.to_s.snake_case }: #{ value.rid } >"
when Array
value.map{|x| x.to_s}
else
value.from_db
@@ -393,17 +392,17 @@
"%s : %s" % [ attr, v] unless v.nil?
end.compact.sort.join(', ') + ">".gsub('"' , ' ')
end
def update **args
- Arcade::Query.new( from: rid , kind: :update, set: args).execute
+ Query.new( from: rid , kind: :update, set: args).execute
refresh
end
# inserts or updates a embedded document
def insert_document name, obj
- value = if obj.is_a? Arcade::Document
+ value = if obj.is_a? Document
obj.to_json
else
obj.to_or
end
# if send( name ).nil? || send( name ).empty?
@@ -415,10 +414,10 @@
def update_embedded embedded, embedded_property, value
db.execute{ " update #{rid} set `#{embedded}`.`#{embedded_property}` = #{value.to_or}" }
end
def update_list list, value
- value = if value.is_a? Arcade::Document
+ value = if value.is_a? Document
value.to_json
else
value.to_or
end
if send( list ).nil? || send( list ).empty?