Class: Arnoldb::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/arnoldb/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Base

Returns a new instance of Base


4
5
6
# File 'lib/arnoldb/base.rb', line 4

def initialize(connection)
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection


3
4
5
# File 'lib/arnoldb/base.rb', line 3

def connection
  @connection
end

Instance Method Details

#create_field(object_type_id, title, value_type) ⇒ String

Creates a Column in Arnoldb

Parameters:

  • object_type_id (String)

    the Table's Arnoldb ID

  • title (String)

    the title of the Column to be created

  • value_type (String)

    the data type for the Column

Returns:

  • (String)

    returns the associated Arnoldb ID for the created Column


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/arnoldb/base.rb', line 24

def create_field(object_type_id, title, value_type)
  field_id = connection.set_field(
    Proto::Field.new(
      object_type_id: object_type_id,
      title: title,
      value_type: value_type
    )
  )["id"]

  field_id
end

#create_object(object_type_id, object_id = "") ⇒ String

Creates an Object in Arnoldb migrations

Parameters:

  • object_type_id (String)

    the Table's Arnoldb ID

  • object_id (String) (defaults to: "")

    the Object's Arnoldb ID used for setting during

Returns:

  • (String)

    returns the associated Arnoldb ID for the created Object


41
42
43
44
45
46
47
48
49
50
# File 'lib/arnoldb/base.rb', line 41

def create_object(object_type_id, object_id = "")
  response = connection.set_object(
    Proto::Object.new(
      object_type_id: object_type_id,
      id: object_id
    )
  )["id"]

  response
end

#create_object_type(title) ⇒ String

Creates a Table in Arnoldb

Parameters:

  • title (String)

    the title of the Table to be created

Returns:

  • (String)

    returns the associated Arnoldb ID for the created Table


11
12
13
14
15
16
17
# File 'lib/arnoldb/base.rb', line 11

def create_object_type(title)
  object_type_id = connection.set_object_type(
    Proto::ObjectType.new(title: title)
  )["id"]

  object_type_id
end

#create_values(values, effective_date = 0) ⇒ Array<Hash>

Creates values in Arnoldb for specific Object Columns take effect in Arnoldb

Parameters:

  • values (Array<Hash>)

    the values which will be created in Arnoldb

  • effective_date (Integer) (defaults to: 0)

    the effective date that the value will

  • objects (Hash)

    a customizable set of options

Options Hash (values):

  • :object_id (String)

    the Arnoldb ID for the Object

  • :object_type_id (String)

    the Arnoldb ID for the Object Type

  • :field_id (String)

    the Arnoldb ID for the Column

  • :value (String)

    the new value

Returns:

  • (Array<Hash>)

    objects the object IDs matched with the new values


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/arnoldb/base.rb', line 64

def create_values(values, effective_date = 0)
  values_messages = []
  values.each do |value|
    field = Proto::Field.new(
      id: value[:field_id],
      object_type_id: value[:object_type_id]
    )
    values_messages << Proto::Value.new(
      object_id: value[:object_id],
      value: value[:value].to_s,
      field: field,
      effective_date: effective_date
    )
  end

  response = connection.set_values(
    Proto::Values.new(
      values: values_messages
    )
  )
  objects = []
  response.values.each do |value|
    objects << { id: value["object_id"], value: value["value"] }
  end

  objects
end

#get_all_object_typesArray<Hash>

Gets the Arnoldb IDs for all of the Object Types in Arnoldb

Parameters:

  • object_types (Hash)

    a customizable set of options

Returns:

  • (Array<Hash>)

    object_types the Object Type IDs and titles


110
111
112
113
114
115
116
117
118
# File 'lib/arnoldb/base.rb', line 110

def get_all_object_types
  object_types = []
  response = connection.get_all_object_types(Proto::Empty.new)
  response.object_types.each do |object_type|
    object_types << { id: object_type.id, title: object_type.title }
  end

  object_types
end

#get_field(field_id) ⇒ Hash

Gets a Field from its Arnoldb ID

Parameters:

  • field_id (String)

    the Arnoldb ID for the Field

  • fields (Hash)

    a customizable set of options

Returns:

  • (Hash)

    fields the Field ID, Object Type ID, title, and value type


127
128
129
130
131
132
133
134
135
136
137
# File 'lib/arnoldb/base.rb', line 127

def get_field(field_id)
  field = connection.get_field(Proto::Field.new(id: field_id))
  result = {
    id: field.id,
    title: field.title,
    value_type: field.value_type,
    object_type_id: field.object_type_id
  }

  result
end

#get_fields(object_type_id) ⇒ Array<Hash>

Gets Fields for an Object Type from Arnoldb

Parameters:

  • object_type_id (String)

    the Arnoldb ID for the Table

  • fields (Hash)

    a customizable set of options

Returns:

  • (Array<Hash>)

    fields the Field IDs, titles, and value types


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/arnoldb/base.rb', line 145

def get_fields(object_type_id)
  fields = []
  response = connection.get_fields(
    Proto::ObjectType.new(id: object_type_id)
  )
  response.fields.each do |field|
    fields << {
      id: field.id,
      title: field.title,
      value_type: field.value_type
    }
  end

  fields
end

#get_object_type(arnoldb_id) ⇒ String?

TODO:

Should ARNOLDB allow titles to be sent

Gets the Arnoldb ID of specific Object Type

Parameters:

  • title (String)

    the title of the Object Type

Returns:

  • (String, nil)

    the Arnoldb ID for the Object Type if found


97
98
99
100
101
102
103
104
# File 'lib/arnoldb/base.rb', line 97

def get_object_type(arnoldb_id)
  object_type = connection.get_object_type(
    Proto::ObjectType.new(id: arnoldb_id)
  )
  object_type = { id: object_type.id, title: object_type.title }

  object_type
end

#get_objects(object_type_id, clauses, date = 0) ⇒ Array<Hash>

TODO:

THIS NEEDS TO BE UPDATED TO HANDLE ORs

Gets Objects from Arnoldb which match specific Query Clauses for a given date. queried

Parameters:

  • object_type_id (String)

    the Arnoldb ID for the Table

  • clauses (Array<Hash>)

    the clauses for querying for Objects

  • date (DateTime) (defaults to: 0)

    the date for what version of the Objects being

Returns:

  • (Array<Hash>)

    Objects which satisfy the clauses


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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/arnoldb/base.rb', line 204

def get_objects(object_type_id, clauses, date = 0)
  if clauses.count > 1
    leaves = []
    clauses.each do |clause|
      leaf = Proto::Objects::Clause::Leaf.new(
        field: clause[:field_id],
        value: clause[:value].to_s,
        cop: clause[:operator]
      )
      leaf = Proto::Objects::Clause.new(l: leaf)
      leaves << leaf
    end

    branches = []
    while leaves.count > 0 do
      left = leaves.pop
      right = branches.empty? ? leaves.pop : branches.pop

    # @todo HARD CODED LOGICAL OPERATOR AS "1" WHICH is AND
      branch = Proto::Objects::Clause::Branch.new(
        lop: 1,
        left: left,
        right: right
      )
      branch = Proto::Objects::Clause.new(b: branch)
      branches << branch
    end

    clause_messages = [Proto::Objects::Clause.new(b: branches.pop)]
  elsif clauses.count == 1
    clause = clauses.pop
    leaf = Proto::Objects::Clause::Leaf.new(
      field: clause[:field_id],
      value: clause[:value].to_s,
      cop: clause[:operator]
    )
    clause_messages = [Proto::Objects::Clause.new(l: leaf)]
  else
    clause_messages = []
  end

  objects_query = Proto::Objects.new(
    object_type_id: object_type_id,
    clauses: clause_messages,
    date: date.to_i
  )

  response = connection.get_objects(objects_query)
  objects = []
  response.objects.each do |object|
    values = []
    object.values.each do |value|
      values << {
        value: value["value"],
        field_id: value.field["id"],
        title: value.field["title"],
        value_type: value.field["value_type"]
      }
    end
    objects << {
      id: object["id"],
      object_type_id: object["object_type_id"],
      values: values
    }
  end

  objects
end

#get_values(object_type_id, object_ids, field_ids, date = 0) ⇒ Array<Hash>

Gets Values from Arnoldb which match the given Object Type ID, Object IDs, and Field IDs queried

Parameters:

  • object_type_id (String)

    the Arnoldb ID for the Table

  • object_ids (Array<String>)

    the Arnoldb IDs for the desired Objects

  • field_ids (Array<String>)

    the Arnoldb IDS for the desired Fields

  • date (DateTime) (defaults to: 0)

    the date for what version of the Values being

Returns:

  • (Array<Hash>)

    Values for the desired Objects and Fields


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/arnoldb/base.rb', line 169

def get_values(object_type_id, object_ids, field_ids, date = 0)
  objects = []
  object_ids.each do |object_id|
    objects << Proto::Object.new(id: object_id)
  end

  fields = []
  field_ids.each do |field_id|
    fields << Proto::Field.new(id: field_id)
  end

  values = Proto::Values.new(
    object_type_id: object_type_id,
    object_ids: objects,
    fields: fields,
    date: date
  )
  response = connection.get_values(values)
  result = []
  response.values.each do |value|
    result << { id: value["object_id"], value: value["value"] }
  end

  result
end