Module: Hws::Stores

Defined in:
lib/hws-stores.rb,
lib/generators/hws/stores/install_generator.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Models Classes: InstallGenerator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_store(input) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/hws-stores.rb', line 5

def self.create_store(input)
  allowed_input = input.with_indifferent_access.slice('name', 'description', 'data', 'schema', 'tags')

  if allowed_input.key?('data')
    if allowed_input['data'].is_a?(Numeric)
      allowed_input['value'] = allowed_input.delete('data')
    else
      allowed_input['data'] = allowed_input['data'].with_indifferent_access
    end
  end

  store = Hws::Stores::Models::Store.create(allowed_input)
  store.try(:id)
end

.delete_store(store_id) ⇒ Object



49
50
51
52
53
54
# File 'lib/hws-stores.rb', line 49

def self.delete_store(store_id)
  store = ::Hws::Stores::Models::Store.find(store_id)
  return false if store.nil?

  store.delete.present?
end

.get_store(store_id) ⇒ Object



32
33
34
35
36
# File 'lib/hws-stores.rb', line 32

def self.get_store(store_id)
  store = ::Hws::Stores::Models::Store.find(store_id)
  { id: store.id, name: store.name, description: store.description,
    data: store.value || store.data, tags: store.tags }.with_indifferent_access
end

.increment(store_id, value) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/hws-stores.rb', line 38

def self.increment(store_id, value)
  store = ::Hws::Stores::Models::Store.find(store_id)
  return false if store.nil?

  if store.data.present?
    raise 'Operation allowed only on a value store.'
  end

  store.increment!(:value, value)
end

.update_store(store_id:, data: nil, tags: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hws-stores.rb', line 20

def self.update_store(store_id:, data: nil, tags: nil)
  store = ::Hws::Stores::Models::Store.find(store_id)
  return false if store.nil?

  update_hash = {}

  update_hash[data.is_a?(Numeric) ? 'value' : 'data'] = data if data.present?
  update_hash['tags'] = store.tags.merge(tags) if tags.present?

  store.update!(update_hash)
end

Instance Method Details

#create_owner(name:, description: nil, tags: {}) ⇒ Object



56
57
58
59
# File 'lib/hws-stores.rb', line 56

def create_owner(name:, description: nil, tags: {})
  owner = ::Hws::Stores::Models::Owner.create(name: name, description: description, tags: tags)
  owner.try(:id)
end

#delete_owner(owner_id) ⇒ Object



77
78
79
# File 'lib/hws-stores.rb', line 77

def delete_owner(owner_id)
  ::Hws::Stores::Models::Owner.find_by(owner_id).try(:delete)
end

#get_owner(owner_id) ⇒ Object



73
74
75
# File 'lib/hws-stores.rb', line 73

def get_owner(owner_id)
  ::Hws::Stores::Models::Owner.find_by(owner_id).as_json
end

#update_owner(owner_id:, name: nil, description: nil, tags: {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hws-stores.rb', line 61

def update_owner(owner_id:, name: nil, description: nil, tags: {})
  owner = ::Hws::Stores::Models::Owner.find_by(owner_id)
  return false if owner.nil?

  update_fields = {}
  update_fields['name'] = name if name.present?
  update_fields['description'] = description if description.present?
  update_fields['tags'] = owner.tags.merge(tags) if tags.present?

  owner.update!(update_fields)
end