Class: Card::Virtual

Inherits:
ApplicationRecord show all
Defined in:
mod/virtual/lib/card/virtual.rb

Overview

Model for the card_virtuals table. It provides method to get and store content for virtual cards from the card_virtuals table.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(card, virtual_content = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'mod/virtual/lib/card/virtual.rb', line 14

def create card, virtual_content=nil
  validate_card card
  virtual_content ||= block_given? ? yield : card.generate_virtual_content
  virtual = new left_id: left_id(card), right_id: right_id(card),
                left_key: card.name.left_key,
                content: virtual_content
  virtual.save!
  virtual
end

.create_or_update(card, virtual_content) ⇒ Object



24
25
26
27
28
29
30
# File 'mod/virtual/lib/card/virtual.rb', line 24

def create_or_update card, virtual_content
  if (virtual_card = find_by_card(card))
    virtual_card.update virtual_content
  else
    create card, virtual_content
  end
end

.fetch(card, &block) ⇒ Object



36
37
38
# File 'mod/virtual/lib/card/virtual.rb', line 36

def fetch card, &block
  find_by_card(card) || create(card, &block)
end

.fetch_content(card, &block) ⇒ Object



32
33
34
# File 'mod/virtual/lib/card/virtual.rb', line 32

def fetch_content card, &block
  find_content_by_card(card) || create(card, &block).content
end

.find_by_card(card) ⇒ Object



50
51
52
# File 'mod/virtual/lib/card/virtual.rb', line 50

def find_by_card card
  where_card(card).take
end

.find_content_by_card(card) ⇒ Object



46
47
48
# File 'mod/virtual/lib/card/virtual.rb', line 46

def find_content_by_card card
  where_card(card)&.pluck(:content)&.first
end

.refresh(card) ⇒ Object



40
41
42
43
44
# File 'mod/virtual/lib/card/virtual.rb', line 40

def refresh card
  virtual = find_by_card(card)
  return create(card) unless virtual
  virtual.update card.generate_virtual_content
end

Instance Method Details

#update(new_content) ⇒ Object



8
9
10
11
# File 'mod/virtual/lib/card/virtual.rb', line 8

def update new_content
  update! content: new_content
  new_content
end