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
# 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),
                content: virtual_content
  virtual.save!
  virtual
end

.fetch(card, &block) ⇒ Object



27
28
29
# File 'mod/virtual/lib/card/virtual.rb', line 27

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

.fetch_content(card, &block) ⇒ Object



23
24
25
# File 'mod/virtual/lib/card/virtual.rb', line 23

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

.find_by_card(card) ⇒ Object



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

def find_by_card card
  where_card(card).take
end

.find_content_by_card(card) ⇒ Object



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

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

.refresh(card) ⇒ Object



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

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_attributes! content: new_content
  new_content
end