Class: Arango::Index

Inherits:
Object
  • Object
show all
Includes:
Collection_Return, Helper_Error, Helper_Return
Defined in:
lib/Index.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Collection_Return

#collection=

Methods included from Helper_Return

#return_delete, #return_directly?, #return_element

Methods included from Helper_Error

#satisfy_category?, #satisfy_class?, #warning_deprecated

Constructor Details

#initialize(collection:, body: {}, id: nil, type: "hash", unique: nil, fields:, sparse: nil, geoJson: nil, minLength: nil, deduplicate: nil, cache_name: nil) ⇒ Index

Returns a new instance of Index.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/Index.rb', line 30

def initialize(collection:, body: {}, id: nil, type: "hash", unique: nil,
  fields:, sparse: nil, geoJson: nil, minLength: nil, deduplicate: nil,
  cache_name: nil)
  assign_collection(collection)
  unless cache_name.nil?
    @cache_name = cache_name
    @server.cache.save(:index, cache_name, self)
  end
  body[:type]        ||= type
  body[:id]          ||= id
  body[:sparse]      ||= sparse
  body[:unique]      ||= unique
  body[:fields]      ||= fields.is_a?(String) ? [fields] : fields
  body[:deduplicate] ||= deduplicate
  body[:geoJson]     ||= geoJson
  body[:minLength]   ||= minLength

  assign_attributes(body)
end

Instance Attribute Details

#cache_nameObject

DEFINE ===



52
53
54
# File 'lib/Index.rb', line 52

def cache_name
  @cache_name
end

#collectionObject (readonly)

Returns the value of attribute collection.



54
55
56
# File 'lib/Index.rb', line 54

def collection
  @collection
end

#databaseObject (readonly)

Returns the value of attribute database.



54
55
56
# File 'lib/Index.rb', line 54

def database
  @database
end

#deduplicateObject

DEFINE ===



52
53
54
# File 'lib/Index.rb', line 52

def deduplicate
  @deduplicate
end

#fieldsObject

DEFINE ===



52
53
54
# File 'lib/Index.rb', line 52

def fields
  @fields
end

#geoJsonObject

DEFINE ===



52
53
54
# File 'lib/Index.rb', line 52

def geoJson
  @geoJson
end

#idObject

DEFINE ===



52
53
54
# File 'lib/Index.rb', line 52

def id
  @id
end

#keyObject

DEFINE ===



52
53
54
# File 'lib/Index.rb', line 52

def key
  @key
end

#minLenghtObject

DEFINE ===



52
53
54
# File 'lib/Index.rb', line 52

def minLenght
  @minLenght
end

#serverObject (readonly)

Returns the value of attribute server.



54
55
56
# File 'lib/Index.rb', line 54

def server
  @server
end

#sparseObject

DEFINE ===



52
53
54
# File 'lib/Index.rb', line 52

def sparse
  @sparse
end

#typeObject

Returns the value of attribute type.



54
55
56
# File 'lib/Index.rb', line 54

def type
  @type
end

#uniqueObject

DEFINE ===



52
53
54
# File 'lib/Index.rb', line 52

def unique
  @unique
end

Class Method Details

.new(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/Index.rb', line 9

def self.new(*args)
  hash = args[0]
  super unless hash.is_a?(Hash)
  collection = hash[:collection]
  if collection.is_a?(Arango::Collection) && collection.database.server.active_cache && !hash[:id].nil?
    cache_name = "#{collection.database.name}/#{collection.name}/#{hash[:id]}"
    cached = collection.database.server.cache.cache.dig(:index, cache_name)
    if cached.nil?
      hash[:cache_name] = cache_name
      return super
    else
      body = hash[:body] || {}
      [:type, :sparse, :unique, :fields, :deduplicate, :geoJson,
        :minLength].each{|k| body[k] ||= hash[k]}
      cached.assign_attributes(body)
      return cached
    end
  end
  super
end

Instance Method Details

#body=(result) ⇒ Object Also known as: assign_attributes



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/Index.rb', line 62

def body=(result)
  @body        = result
  @id          = result[:id] || @id
  @key         = @id&.split("/")&.dig(1)
  @type        = assign_type(result[:type] || @type)
  @unique      = result[:unique]      || @unique
  @fields      = result[:fields]      || @fields
  @sparse      = result[:sparse]      || @sparse
  @geoJson     = result[:geoJson]     || @geoJson
  @minLength   = result[:minLength]   || @minLength
  @deduplicate = result[:deduplicate] || @deduplicate
  if @server.active_cache && @cache_name.nil?
    @cache_name = "#{@database.name}/#{@collection.name}/#{@id}"
    @server.cache.save(:index, @cache_name, self)
  end
end

#createObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/Index.rb', line 106

def create
  body = {
    "fields":      @fields,
    "unique":      @unique,
    "type":        @type,
    "id":          @id,
    "geoJson":     @geoJson,
    "minLength":   @minLength,
    "deduplicate": @deduplicate
  }
  query = { "collection": @collection.name }
  result = @database.request("POST", "_api/index", body: body, query: query)
  return_element(result)
end

#destroyObject



121
122
123
124
# File 'lib/Index.rb', line 121

def destroy
  result = @database.request("DELETE", "_api/index/#{@id}")
  return_delete(result)
end

#retrieveObject

COMMANDS ===



101
102
103
104
# File 'lib/Index.rb', line 101

def retrieve
  result = @database.request("GET", "_api/index/#{@id}")
  return_element(result)
end

#to_hObject

DEFINE ===



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/Index.rb', line 82

def to_h
  {
    "key": @key,
    "id": @id,
    "body": @body,
    "type": @type,
    "sparse": @sparse,
    "unique": @unique,
    "fields": @fields,
    "idCache": @idCache,
    "geoJson": @geoJson,
    "minLength": @minLength,
    "deduplicate": @deduplicate,
    "collection": @collection.name
  }.delete_if{|k,v| v.nil?}
end