Class: Kybus::Storage::Datasource::DynamoidRepository

Inherits:
Repository
  • Object
show all
Defined in:
lib/kybus/storage/datasource/dynamoid.rb

Overview

Repository that fetches and stores objects using a Dynamoid connection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, id) ⇒ DynamoidRepository

Returns a new instance of DynamoidRepository.



43
44
45
46
# File 'lib/kybus/storage/datasource/dynamoid.rb', line 43

def initialize(model_class, id)
  @model_class = model_class
  super(id, IDGenerators[:id])
end

Instance Attribute Details

#model_classObject (readonly)

Returns the value of attribute model_class.



8
9
10
# File 'lib/kybus/storage/datasource/dynamoid.rb', line 8

def model_class
  @model_class
end

Class Method Details

.create_dynamic_model(conf) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kybus/storage/datasource/dynamoid.rb', line 26

def self.create_dynamic_model(conf)
  Class.new do
    include Dynamoid::Document

    table name: conf['table'], key: conf['primary_key'].to_sym, read_capacity: conf['read_capacity'],
          write_capacity: conf['write_capacity']

    # Dynamically add fields based on configuration
    conf['fields'].each do |field, type|
      field field.to_sym, type.to_sym
    end

    # Set a constant name for the dynamic class
    class_eval { const_set("DynamicModel#{conf['table'].upcase}", self) }
  end
end

.from_config(conf) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/kybus/storage/datasource/dynamoid.rb', line 10

def self.from_config(conf)
  if conf['dynamoid_config']
    require 'dynamoid'
    Dynamoid.configure do |config|
      config.access_key = conf['access_key']
      config.secret_key = conf['secret_key']
      config.region = conf['region']
      config.namespace = conf['namespace']
      config.endpoint = conf['endpoint'] if conf['endpoint']
    end
  end

  model_class = conf['schema'] || create_dynamic_model(conf)
  new(model_class, conf['primary_key'].to_sym)
end

Instance Method Details

#connectionObject



70
71
72
# File 'lib/kybus/storage/datasource/dynamoid.rb', line 70

def connection
  @model_class.connection
end

#create_(data) ⇒ Object



57
58
59
60
# File 'lib/kybus/storage/datasource/dynamoid.rb', line 57

def create_(data)
  obj = @model_class.create(data)
  obj.attributes
end

#get(id) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/kybus/storage/datasource/dynamoid.rb', line 48

def get(id)
  result = @model_class.find(id)
  raise(ObjectNotFound, id) if result.nil?

  result.attributes
rescue Dynamoid::Errors::RecordNotFound
  raise(ObjectNotFound, id)
end

#store(data) ⇒ Object

Raises:

  • (ObjectNotFound)


62
63
64
65
66
67
68
# File 'lib/kybus/storage/datasource/dynamoid.rb', line 62

def store(data)
  obj = @model_class.find(data[@id])
  raise(ObjectNotFound, data[@id]) if obj.nil?

  obj.update_attributes(data)
  obj.attributes
end