Module: Diametric::Persistence::REST::ClassMethods

Included in:
Diametric::Persistence::REST
Defined in:
lib/diametric/persistence/rest.rb

Instance Method Summary collapse

Instance Method Details

#allObject


58
59
60
# File 'lib/diametric/persistence/rest.rb', line 58

def all
  Diametric::Query.new(self, nil, true).all
end

#connect(options = {}) ⇒ Object


29
30
31
32
33
34
35
36
# File 'lib/diametric/persistence/rest.rb', line 29

def connect(options = {})
  @uri = options[:uri]
  @dbalias = options[:storage]
  @database = options[:database]

  @connection = Datomic::Client.new(@uri, @dbalias)
  @connection.create_database(@database)
end

#connectionObject


38
39
40
# File 'lib/diametric/persistence/rest.rb', line 38

def connection
  @connection || Diametric::Persistence::REST.connection
end

#create_schemaObject


54
55
56
# File 'lib/diametric/persistence/rest.rb', line 54

def create_schema
  transact(schema)
end

#databaseObject


42
43
44
# File 'lib/diametric/persistence/rest.rb', line 42

def database
  @database || Diametric::Persistence::REST.database
end

#filter(*filter) ⇒ Object


71
72
73
74
# File 'lib/diametric/persistence/rest.rb', line 71

def filter(*filter)
  query = Diametric::Query.new(self, nil, true)
  query.filter(*filter)
end

#first(conditions = {}) ⇒ Object


62
63
64
# File 'lib/diametric/persistence/rest.rb', line 62

def first(conditions = {})
  where(conditions).first
end

#get(dbid, conn = nil, resolve = false) ⇒ Object


76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/diametric/persistence/rest.rb', line 76

def get(dbid, conn=nil, resolve=false)
  conn ||= connection
  res = conn.entity(database, dbid.to_i)

  # TODO tighten regex to only allow fields with the model name
  attrs = res.data.map { |attr_symbol, value|
    attr = attr_symbol.to_s.gsub(%r"^\w+/", '')
    [attr, value]
  }

  entity = self.new(Hash[*attrs.flatten])
  entity.dbid = dbid
  entity
end

#q(query, args, unused = nil) ⇒ Object


91
92
93
94
95
# File 'lib/diametric/persistence/rest.rb', line 91

def q(query, args, unused=nil)
  args.unshift(connection.db_alias(database))
  res = connection.query(query, args)
  res.data
end

#tempid(*e) ⇒ Object


46
47
48
# File 'lib/diametric/persistence/rest.rb', line 46

def tempid(*e)
  EDN.tagged_element('db/id', e)
end

#transact(data) ⇒ Object


50
51
52
# File 'lib/diametric/persistence/rest.rb', line 50

def transact(data)
  connection.transact(database, data)
end

#where(conditions = {}) ⇒ Object


66
67
68
69
# File 'lib/diametric/persistence/rest.rb', line 66

def where(conditions = {})
  query = Diametric::Query.new(self, nil, true)
  query.where(conditions)
end