Class: Card::Virtual

Inherits:
ApplicationRecord show all
Defined in:
card-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
# File 'card-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
  create! left_id: left_id(card),
          right_id: right_id(card),
          left_key: card.name.left_key,
          content: virtual_content
end

.create_or_update(card, virtual_content) ⇒ Object



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

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



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

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

.fetch_content(card, &block) ⇒ Object



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

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

.find_by_card(card) ⇒ Object



50
51
52
# File 'card-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 'card-mod-virtual/lib/card/virtual.rb', line 46

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

.refresh(card) ⇒ Object



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

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 'card-mod-virtual/lib/card/virtual.rb', line 8

def update new_content
  update! content: new_content
  new_content
end