card-mod-virtual 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7c8449ba0faf338250ca2b24c377c0fd6feb0c3c5e2933f3938c533f2e9a2db4
4
+ data.tar.gz: b3fbb673b528e19c1ac0ad124b70c474257ca95ec9a8a0dab05329df85a71aff
5
+ SHA512:
6
+ metadata.gz: 1afad2d1d4b9f30944a6e3d72dfd2628512dd121742a26cb4c3ac159db01d122bf2009bd9f6edac72733e48b13d20575f95105df5e91cbda0b190b137456e78a
7
+ data.tar.gz: '02529a4f4ee5011e2de00ec5015d77f84535c234b561908d0ddedf829ca25d44b79a32312fd86273e78e83d8fad8ba43985b8b50f7f6b57b8b2fca626f7c05dc'
@@ -0,0 +1,95 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ class Card
4
+ # Model for the card_virtuals table.
5
+ # It provides method to get and store content for virtual cards from
6
+ # the card_virtuals table.
7
+ class Virtual < ApplicationRecord
8
+ def update new_content
9
+ update! content: new_content
10
+ new_content
11
+ end
12
+
13
+ class << self
14
+ def create card, virtual_content=nil
15
+ validate_card card
16
+ virtual_content ||= block_given? ? yield : card.generate_virtual_content
17
+ virtual = new left_id: left_id(card), right_id: right_id(card),
18
+ left_key: card.name.left_key,
19
+ content: virtual_content
20
+ virtual.save!
21
+ virtual
22
+ end
23
+
24
+ def create_or_update card, virtual_content
25
+ if (virtual_card = find_by_card(card))
26
+ virtual_card.update virtual_content
27
+ else
28
+ create card, virtual_content
29
+ end
30
+ end
31
+
32
+ def fetch_content card, &block
33
+ find_content_by_card(card) || create(card, &block).content
34
+ end
35
+
36
+ def fetch card, &block
37
+ find_by_card(card) || create(card, &block)
38
+ end
39
+
40
+ def refresh card
41
+ virtual = find_by_card(card)
42
+ return create(card) unless virtual
43
+ virtual.update card.generate_virtual_content
44
+ end
45
+
46
+ def find_content_by_card card
47
+ where_card(card)&.pluck(:content)&.first
48
+ end
49
+
50
+ def find_by_card card
51
+ where_card(card).take
52
+ end
53
+
54
+ private
55
+
56
+ def where_card card
57
+ query = { right_id: right_id(card) }
58
+ if (lid = left_id(card))
59
+ query[:left_id] = lid
60
+ else
61
+ query[:left_key] = card.name.left_key
62
+ end
63
+ where query
64
+ end
65
+
66
+ def left_id card
67
+ if card.junction?
68
+ card.left_id&.positive? ? card.left_id : card.left&.id
69
+ else
70
+ card.id
71
+ end
72
+ end
73
+
74
+ def right_id card
75
+ if card.junction?
76
+ card.right_id&.positive? ? card.right_id : card.right&.id
77
+ else
78
+ -2
79
+ end
80
+ end
81
+
82
+ def validate_card card
83
+ reason ||=
84
+ if card.junction?
85
+ "needs left_id" unless left_id(card)
86
+ "needs right_id" unless right_id(card)
87
+ elsif !card.id
88
+ "needs id"
89
+ end
90
+ return unless reason
91
+ raise Card::Error, card.name, "count not cacheable: card #{card.name} #{reason}"
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,43 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ def virtual?
4
+ new?
5
+ end
6
+
7
+ def history?
8
+ false
9
+ end
10
+
11
+ def followable?
12
+ false
13
+ end
14
+
15
+ def db_content
16
+ Card::Virtual.fetch_content(self)
17
+ end
18
+
19
+ # called to refresh the virtual content
20
+ # the default way is to use the card's template content
21
+ def generate_virtual_content
22
+ template&.db_content
23
+ end
24
+
25
+ event :save_virtual_content, :prepare_to_store, on: :save, changed: :content do
26
+ Card::Virtual.create_or_update(self, attributes["db_content"])
27
+ abort :success
28
+ end
29
+
30
+ event :delete_virtual_content, :prepare_to_store, on: :delete do
31
+ Card::Virtual.find_by_card(self)&.delete
32
+ abort :success
33
+ end
34
+
35
+ def delete
36
+ # delete although it's new
37
+ update trash: true
38
+ end
39
+
40
+ def delete!
41
+ # delete although it's new
42
+ update! trash: true
43
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: card-mod-virtual
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.11.0
5
+ platform: ruby
6
+ authors:
7
+ - Ethan McCutchen
8
+ - Philipp Kühl
9
+ - Gerry Gleason
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2020-12-24 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: card
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.101.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - '='
27
+ - !ruby/object:Gem::Version
28
+ version: 1.101.0
29
+ description: ''
30
+ email:
31
+ - info@decko.org
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - lib/card/virtual.rb
37
+ - set/abstract/virtual_cache.rb
38
+ homepage: http://decko.org
39
+ licenses:
40
+ - GPL-3.0
41
+ metadata:
42
+ card-mod: virtual
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '2.5'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.0.3
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: virtual
62
+ test_files: []