Module: Diametric::Persistence::REST

Extended by:
ClassMethods
Defined in:
lib/diametric/persistence/rest.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

all, connect, connection, create_schema, database, filter, first, get, q, tempid, transact, where

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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


132
133
134
135
136
137
# File 'lib/diametric/persistence/rest.rb', line 132

def method_missing(method_name, *args, &block)
  functions = self.instance_variable_get("@transaction_functions")
  if functions && functions.include?(method_name)
    return invoke_function(method_name, args)
  end
end

Class Method Details

.connectionObject


18
19
20
# File 'lib/diametric/persistence/rest.rb', line 18

def self.connection
  @connection
end

.create_schemasObject


22
23
24
25
26
# File 'lib/diametric/persistence/rest.rb', line 22

def self.create_schemas
  @persisted_classes.each do |klass|
    klass.create_schema
  end
end

.included(base) ⇒ Object


12
13
14
15
16
# File 'lib/diametric/persistence/rest.rb', line 12

def self.included(base)
  base.send(:include, Diametric::Persistence::Common)
  base.send(:extend, ClassMethods)
  @persisted_classes.add(base)
end

Instance Method Details

#invoke_function(method_name, args) ⇒ Object


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/diametric/persistence/rest.rb', line 139

def invoke_function(method_name, args)
  params = args.dup
  conn = params.shift
  conn ||= Diametric::Persistence::REST.connection
  attribute_names = self.class.attribute_names
  params = params.map do |e|
    if attribute_names.include?(e)
      e = (self.class.prefix + "/" + e.to_s).to_sym
    else
      e
    end
  end
  conn.transact(Diametric::Persistence::REST.database, [[method_name, self.dbid, *params]])
  self.class.reify(self.dbid, conn)
end

#retract_entity(dbid) ⇒ Object


127
128
129
130
# File 'lib/diametric/persistence/rest.rb', line 127

def retract_entity(dbid)
  query = [[:"db.fn/retractEntity", dbid.to_i]]
  self.class.transact(query)
end

#savetrue, false

Save the entity

Examples:

Save the entity.

entity.save

Returns:

  • (true, false)

    True is success, false if not.


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/diametric/persistence/rest.rb', line 106

def save
  return false unless valid?
  return true unless changed?

  self.dbid = self.dbid.to_i if self.dbid.class == String

  res = self.class.transact(tx_data)
  if dbid.nil? || dbid.is_a?(EDN::Type::Unknown)
    self.dbid = res.data[:tempids].values.first
  end

  @previously_changed = changes
  @changed_attributes.clear

  res
end

#to_ednObject


123
124
125
# File 'lib/diametric/persistence/rest.rb', line 123

def to_edn
  self.dbid
end