carddb-rails 0.1.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +24 -0
- data/LICENSE.txt +21 -0
- data/README.md +494 -0
- data/lib/carddb/rails/configuration.rb +16 -0
- data/lib/carddb/rails/controller_helpers.rb +11 -0
- data/lib/carddb/rails/deck_controller_helpers.rb +27 -0
- data/lib/carddb/rails/deck_export_service.rb +21 -0
- data/lib/carddb/rails/deck_import_service.rb +20 -0
- data/lib/carddb/rails/deck_sync_job.rb +15 -0
- data/lib/carddb/rails/deck_sync_service.rb +21 -0
- data/lib/carddb/rails/graphql/dataloader/dataset_source.rb +37 -0
- data/lib/carddb/rails/graphql/dataloader/deck_by_external_ref_source.rb +26 -0
- data/lib/carddb/rails/graphql/dataloader/deck_source.rb +26 -0
- data/lib/carddb/rails/graphql/dataloader/game_source.rb +30 -0
- data/lib/carddb/rails/graphql/dataloader/publisher_source.rb +26 -0
- data/lib/carddb/rails/graphql/dataloader/record_source.rb +39 -0
- data/lib/carddb/rails/graphql/dataset_loader.rb +41 -0
- data/lib/carddb/rails/graphql/deck_loader.rb +51 -0
- data/lib/carddb/rails/graphql/game_loader.rb +34 -0
- data/lib/carddb/rails/graphql/helpers.rb +220 -0
- data/lib/carddb/rails/graphql/publisher_loader.rb +30 -0
- data/lib/carddb/rails/graphql/record_loader.rb +43 -0
- data/lib/carddb/rails/has_carddb_dataset.rb +89 -0
- data/lib/carddb/rails/has_carddb_datasets.rb +105 -0
- data/lib/carddb/rails/has_carddb_deck.rb +134 -0
- data/lib/carddb/rails/has_carddb_game.rb +84 -0
- data/lib/carddb/rails/has_carddb_publisher.rb +75 -0
- data/lib/carddb/rails/has_carddb_record.rb +166 -0
- data/lib/carddb/rails/railtie.rb +34 -0
- data/lib/carddb/rails/resolver.rb +54 -0
- data/lib/carddb/rails/test_helper.rb +12 -0
- data/lib/carddb/rails/version.rb +7 -0
- data/lib/carddb/rails.rb +93 -0
- data/lib/carddb-rails.rb +3 -0
- data/lib/generators/carddb/install/install_generator.rb +15 -0
- data/lib/generators/carddb/install/templates/carddb.rb +23 -0
- metadata +142 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/concern'
|
|
4
|
+
require 'active_support/core_ext/object/blank'
|
|
5
|
+
|
|
6
|
+
module CardDB
|
|
7
|
+
module Rails
|
|
8
|
+
module HasCardDBDataset
|
|
9
|
+
extend ActiveSupport::Concern
|
|
10
|
+
|
|
11
|
+
included do
|
|
12
|
+
class_attribute :carddb_dataset_definitions, instance_accessor: false, default: {}
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class_methods do
|
|
16
|
+
def has_carddb_dataset(name = :carddb_dataset, dataset_key:, publisher_slug: nil, game_key: nil, client: nil,
|
|
17
|
+
cache: nil)
|
|
18
|
+
name = name.to_sym
|
|
19
|
+
definition = {
|
|
20
|
+
dataset_key: dataset_key,
|
|
21
|
+
publisher_slug: publisher_slug,
|
|
22
|
+
game_key: game_key,
|
|
23
|
+
client: client,
|
|
24
|
+
cache: cache
|
|
25
|
+
}.freeze
|
|
26
|
+
|
|
27
|
+
self.carddb_dataset_definitions = carddb_dataset_definitions.merge(name => definition)
|
|
28
|
+
|
|
29
|
+
define_method(name) do
|
|
30
|
+
carddb_load_dataset(name)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
define_method("#{name}!") do
|
|
34
|
+
public_send(name) || raise(CardDB::NotFoundError, "No CardDB dataset found for #{self.class.name}##{name}")
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
define_method("reload_#{name}") do
|
|
38
|
+
carddb_dataset_store.delete(name)
|
|
39
|
+
public_send(name)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
define_method("#{name}_loaded?") do
|
|
43
|
+
carddb_dataset_store.key?(name)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
def carddb_load_dataset(name)
|
|
51
|
+
return carddb_dataset_store[name] if carddb_dataset_store.key?(name)
|
|
52
|
+
|
|
53
|
+
definition = self.class.carddb_dataset_definitions.fetch(name)
|
|
54
|
+
client = carddb_dataset_resolve_client(definition[:client])
|
|
55
|
+
dataset_key = carddb_dataset_resolve_required_option(definition[:dataset_key], 'dataset_key')
|
|
56
|
+
publisher_slug = client.config.resolve_publisher(carddb_dataset_resolve_option(definition[:publisher_slug]))
|
|
57
|
+
game_key = client.config.resolve_game(carddb_dataset_resolve_option(definition[:game_key]))
|
|
58
|
+
|
|
59
|
+
raise ArgumentError, 'publisher_slug is required (no default configured)' if publisher_slug.blank?
|
|
60
|
+
raise ArgumentError, 'game_key is required (no default configured)' if game_key.blank?
|
|
61
|
+
|
|
62
|
+
record = client.datasets.get(
|
|
63
|
+
publisher_slug: publisher_slug,
|
|
64
|
+
game_key: game_key,
|
|
65
|
+
dataset_key: dataset_key,
|
|
66
|
+
cache: definition[:cache]
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
carddb_dataset_store[name] = record
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def carddb_dataset_store
|
|
73
|
+
@carddb_dataset_store ||= {}
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def carddb_dataset_resolve_client(option)
|
|
77
|
+
CardDB::Rails::Resolver.resolve_client(self, option)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def carddb_dataset_resolve_required_option(option, name)
|
|
81
|
+
CardDB::Rails::Resolver.resolve_required_option(self, option, name)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def carddb_dataset_resolve_option(option)
|
|
85
|
+
CardDB::Rails::Resolver.resolve_option(self, option)
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/concern'
|
|
4
|
+
require 'active_support/core_ext/object/blank'
|
|
5
|
+
|
|
6
|
+
module CardDB
|
|
7
|
+
module Rails
|
|
8
|
+
module HasCardDBDatasets
|
|
9
|
+
extend ActiveSupport::Concern
|
|
10
|
+
|
|
11
|
+
included do
|
|
12
|
+
class_attribute :carddb_datasets_definition, instance_accessor: false, default: nil
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class_methods do
|
|
16
|
+
def has_carddb_datasets(publisher_slug: nil, game_key: nil, client: nil, cache: nil)
|
|
17
|
+
self.carddb_datasets_definition = {
|
|
18
|
+
publisher_slug: publisher_slug,
|
|
19
|
+
game_key: game_key,
|
|
20
|
+
client: client,
|
|
21
|
+
cache: cache
|
|
22
|
+
}.freeze
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def carddb_datasets(purpose: nil, search: nil, first: nil, after: nil, cache: nil)
|
|
27
|
+
if purpose.nil? && search.nil? && first.nil? && after.nil?
|
|
28
|
+
return carddb_fetch_datasets(cache: cache) if cache == false
|
|
29
|
+
|
|
30
|
+
return @carddb_datasets if defined?(@carddb_datasets)
|
|
31
|
+
|
|
32
|
+
@carddb_datasets = carddb_fetch_datasets(cache: cache)
|
|
33
|
+
else
|
|
34
|
+
carddb_fetch_datasets(purpose: purpose, search: search, first: first, after: after, cache: cache)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def reload_carddb_datasets(cache: nil)
|
|
39
|
+
remove_instance_variable(:@carddb_datasets) if defined?(@carddb_datasets)
|
|
40
|
+
carddb_datasets(cache: cache)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def carddb_datasets_loaded?
|
|
44
|
+
instance_variable_defined?(:@carddb_datasets)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def carddb_dataset(dataset_key, cache: nil)
|
|
48
|
+
if cache != false && carddb_datasets_loaded? && @carddb_datasets.respond_to?(:find)
|
|
49
|
+
matching_dataset = @carddb_datasets.find { |dataset| dataset.key == dataset_key }
|
|
50
|
+
return matching_dataset if matching_dataset
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
definition = self.class.carddb_datasets_definition
|
|
54
|
+
raise ArgumentError, 'has_carddb_datasets must be configured before calling carddb_dataset' unless definition
|
|
55
|
+
|
|
56
|
+
client = carddb_datasets_resolve_client(definition[:client])
|
|
57
|
+
publisher_slug = client.config.resolve_publisher(carddb_datasets_resolve_option(definition[:publisher_slug]))
|
|
58
|
+
game_key = client.config.resolve_game(carddb_datasets_resolve_option(definition[:game_key]))
|
|
59
|
+
|
|
60
|
+
raise ArgumentError, 'publisher_slug is required (no default configured)' if publisher_slug.blank?
|
|
61
|
+
raise ArgumentError, 'game_key is required (no default configured)' if game_key.blank?
|
|
62
|
+
|
|
63
|
+
client.datasets.get(
|
|
64
|
+
publisher_slug: publisher_slug,
|
|
65
|
+
game_key: game_key,
|
|
66
|
+
dataset_key: dataset_key,
|
|
67
|
+
cache: cache.nil? ? definition[:cache] : cache
|
|
68
|
+
)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
def carddb_fetch_datasets(purpose: nil, search: nil, first: nil, after: nil, cache: nil)
|
|
74
|
+
definition = self.class.carddb_datasets_definition
|
|
75
|
+
raise ArgumentError, 'has_carddb_datasets must be configured before calling carddb_datasets' unless definition
|
|
76
|
+
|
|
77
|
+
_cache = cache
|
|
78
|
+
|
|
79
|
+
client = carddb_datasets_resolve_client(definition[:client])
|
|
80
|
+
publisher_slug = client.config.resolve_publisher(carddb_datasets_resolve_option(definition[:publisher_slug]))
|
|
81
|
+
game_key = client.config.resolve_game(carddb_datasets_resolve_option(definition[:game_key]))
|
|
82
|
+
|
|
83
|
+
raise ArgumentError, 'publisher_slug is required (no default configured)' if publisher_slug.blank?
|
|
84
|
+
raise ArgumentError, 'game_key is required (no default configured)' if game_key.blank?
|
|
85
|
+
|
|
86
|
+
client.datasets.search(
|
|
87
|
+
publisher_slug: publisher_slug,
|
|
88
|
+
game_key: game_key,
|
|
89
|
+
purpose: purpose,
|
|
90
|
+
search: search,
|
|
91
|
+
first: first,
|
|
92
|
+
after: after
|
|
93
|
+
)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def carddb_datasets_resolve_client(option)
|
|
97
|
+
CardDB::Rails::Resolver.resolve_client(self, option)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def carddb_datasets_resolve_option(option)
|
|
101
|
+
CardDB::Rails::Resolver.resolve_option(self, option)
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/concern'
|
|
4
|
+
require 'active_support/core_ext/object/blank'
|
|
5
|
+
|
|
6
|
+
module CardDB
|
|
7
|
+
module Rails
|
|
8
|
+
module HasCardDBDeck
|
|
9
|
+
extend ActiveSupport::Concern
|
|
10
|
+
|
|
11
|
+
included do
|
|
12
|
+
class_attribute :carddb_deck_definitions, instance_accessor: false, default: {}
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class_methods do
|
|
16
|
+
def has_carddb_deck(name = :carddb_deck, id: nil, external_ref: nil, client: nil, cache: nil)
|
|
17
|
+
name = name.to_sym
|
|
18
|
+
definition = {
|
|
19
|
+
id: id,
|
|
20
|
+
external_ref: external_ref,
|
|
21
|
+
client: client,
|
|
22
|
+
cache: cache
|
|
23
|
+
}.freeze
|
|
24
|
+
|
|
25
|
+
self.carddb_deck_definitions = carddb_deck_definitions.merge(name => definition)
|
|
26
|
+
|
|
27
|
+
define_method(name) do
|
|
28
|
+
carddb_load_deck(name)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
define_method("#{name}!") do
|
|
32
|
+
public_send(name) || raise(CardDB::NotFoundError, "No CardDB deck found for #{self.class.name}##{name}")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
define_method("reload_#{name}") do
|
|
36
|
+
carddb_deck_store.delete(name)
|
|
37
|
+
public_send(name)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
define_method("#{name}_loaded?") do
|
|
41
|
+
carddb_deck_store.key?(name)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
unless name == :carddb_deck
|
|
45
|
+
define_singleton_method("preload_#{name.to_s.pluralize}") do |records|
|
|
46
|
+
preload_carddb_decks(records, as: name)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def preload_carddb_decks(records, as: :carddb_deck)
|
|
52
|
+
records = Array(records).compact
|
|
53
|
+
return records if records.empty?
|
|
54
|
+
|
|
55
|
+
definition = carddb_deck_definitions.fetch(as.to_sym)
|
|
56
|
+
records.group_by { |record| record.send(:carddb_deck_batch_group_key, definition) }.each_value do |group|
|
|
57
|
+
first_record = group.first
|
|
58
|
+
client = first_record.send(:carddb_deck_resolve_client, definition[:client])
|
|
59
|
+
ids = group.filter_map { |record| record.send(:carddb_deck_resolve_option, definition[:id]) }.uniq
|
|
60
|
+
external_refs = group.filter_map { |record| record.send(:carddb_deck_resolve_option, definition[:external_ref]) }.uniq
|
|
61
|
+
|
|
62
|
+
if ids.any?
|
|
63
|
+
results = client.batch do |batch|
|
|
64
|
+
ids.each do |id|
|
|
65
|
+
batch.decks.get(id)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
decks_by_id = ids.zip(results).to_h
|
|
69
|
+
|
|
70
|
+
group.each do |record|
|
|
71
|
+
id = record.send(:carddb_deck_resolve_option, definition[:id])
|
|
72
|
+
record.send(:carddb_write_deck, as.to_sym, decks_by_id[id]) if id
|
|
73
|
+
end
|
|
74
|
+
elsif external_refs.any?
|
|
75
|
+
results = client.batch do |batch|
|
|
76
|
+
external_refs.each do |external_ref|
|
|
77
|
+
batch.decks.get(external_ref: external_ref)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
decks_by_external_ref = external_refs.zip(results).to_h
|
|
81
|
+
|
|
82
|
+
group.each do |record|
|
|
83
|
+
external_ref = record.send(:carddb_deck_resolve_option, definition[:external_ref])
|
|
84
|
+
record.send(:carddb_write_deck, as.to_sym, decks_by_external_ref[external_ref]) if external_ref
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
records
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
private
|
|
94
|
+
|
|
95
|
+
def carddb_load_deck(name)
|
|
96
|
+
return carddb_deck_store[name] if carddb_deck_store.key?(name)
|
|
97
|
+
|
|
98
|
+
definition = self.class.carddb_deck_definitions.fetch(name)
|
|
99
|
+
id = carddb_deck_resolve_option(definition[:id])
|
|
100
|
+
external_ref = carddb_deck_resolve_option(definition[:external_ref])
|
|
101
|
+
return carddb_write_deck(name, nil) if id.blank? && external_ref.blank?
|
|
102
|
+
|
|
103
|
+
client = carddb_deck_resolve_client(definition[:client])
|
|
104
|
+
deck = if id.present?
|
|
105
|
+
client.decks.get(id, cache: definition[:cache])
|
|
106
|
+
else
|
|
107
|
+
client.decks.get_by_external_ref(external_ref: external_ref, cache: definition[:cache])
|
|
108
|
+
end
|
|
109
|
+
carddb_write_deck(name, deck)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def carddb_write_deck(name, deck)
|
|
113
|
+
carddb_deck_store[name] = deck
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def carddb_deck_store
|
|
117
|
+
@carddb_deck_store ||= {}
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def carddb_deck_batch_group_key(definition)
|
|
121
|
+
client = carddb_deck_resolve_client(definition[:client])
|
|
122
|
+
[client.object_id]
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def carddb_deck_resolve_client(option)
|
|
126
|
+
CardDB::Rails::Resolver.resolve_client(self, option)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def carddb_deck_resolve_option(option)
|
|
130
|
+
CardDB::Rails::Resolver.resolve_option(self, option)
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/concern'
|
|
4
|
+
require 'active_support/core_ext/object/blank'
|
|
5
|
+
|
|
6
|
+
module CardDB
|
|
7
|
+
module Rails
|
|
8
|
+
module HasCardDBGame
|
|
9
|
+
extend ActiveSupport::Concern
|
|
10
|
+
|
|
11
|
+
included do
|
|
12
|
+
class_attribute :carddb_game_definitions, instance_accessor: false, default: {}
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class_methods do
|
|
16
|
+
def has_carddb_game(name = :carddb_game, game_key:, publisher_slug: nil, client: nil, cache: nil)
|
|
17
|
+
name = name.to_sym
|
|
18
|
+
definition = {
|
|
19
|
+
game_key: game_key,
|
|
20
|
+
publisher_slug: publisher_slug,
|
|
21
|
+
client: client,
|
|
22
|
+
cache: cache
|
|
23
|
+
}.freeze
|
|
24
|
+
|
|
25
|
+
self.carddb_game_definitions = carddb_game_definitions.merge(name => definition)
|
|
26
|
+
|
|
27
|
+
define_method(name) do
|
|
28
|
+
carddb_load_game(name)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
define_method("#{name}!") do
|
|
32
|
+
public_send(name) || raise(CardDB::NotFoundError, "No CardDB game found for #{self.class.name}##{name}")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
define_method("reload_#{name}") do
|
|
36
|
+
carddb_game_store.delete(name)
|
|
37
|
+
public_send(name)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
define_method("#{name}_loaded?") do
|
|
41
|
+
carddb_game_store.key?(name)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def carddb_load_game(name)
|
|
49
|
+
return carddb_game_store[name] if carddb_game_store.key?(name)
|
|
50
|
+
|
|
51
|
+
definition = self.class.carddb_game_definitions.fetch(name)
|
|
52
|
+
client = carddb_game_resolve_client(definition[:client])
|
|
53
|
+
game_key = carddb_game_resolve_required_option(definition[:game_key], 'game_key')
|
|
54
|
+
publisher_slug = client.config.resolve_publisher(carddb_game_resolve_option(definition[:publisher_slug]))
|
|
55
|
+
|
|
56
|
+
raise ArgumentError, 'publisher_slug is required (no default configured)' if publisher_slug.blank?
|
|
57
|
+
|
|
58
|
+
record = client.games.get(
|
|
59
|
+
publisher_slug: publisher_slug,
|
|
60
|
+
game_key: game_key,
|
|
61
|
+
cache: definition[:cache]
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
carddb_game_store[name] = record
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def carddb_game_store
|
|
68
|
+
@carddb_game_store ||= {}
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def carddb_game_resolve_client(option)
|
|
72
|
+
CardDB::Rails::Resolver.resolve_client(self, option)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def carddb_game_resolve_required_option(option, name)
|
|
76
|
+
CardDB::Rails::Resolver.resolve_required_option(self, option, name)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def carddb_game_resolve_option(option)
|
|
80
|
+
CardDB::Rails::Resolver.resolve_option(self, option)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/concern'
|
|
4
|
+
require 'active_support/core_ext/object/blank'
|
|
5
|
+
|
|
6
|
+
module CardDB
|
|
7
|
+
module Rails
|
|
8
|
+
module HasCardDBPublisher
|
|
9
|
+
extend ActiveSupport::Concern
|
|
10
|
+
|
|
11
|
+
included do
|
|
12
|
+
class_attribute :carddb_publisher_definitions, instance_accessor: false, default: {}
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class_methods do
|
|
16
|
+
def has_carddb_publisher(name = :carddb_publisher, slug: nil, id: nil, client: nil, cache: nil)
|
|
17
|
+
name = name.to_sym
|
|
18
|
+
definition = {
|
|
19
|
+
slug: slug,
|
|
20
|
+
id: id,
|
|
21
|
+
client: client,
|
|
22
|
+
cache: cache
|
|
23
|
+
}.freeze
|
|
24
|
+
|
|
25
|
+
self.carddb_publisher_definitions = carddb_publisher_definitions.merge(name => definition)
|
|
26
|
+
|
|
27
|
+
define_method(name) do
|
|
28
|
+
carddb_load_publisher(name)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
define_method("#{name}!") do
|
|
32
|
+
public_send(name) || raise(CardDB::NotFoundError, "No CardDB publisher found for #{self.class.name}##{name}")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
define_method("reload_#{name}") do
|
|
36
|
+
carddb_publisher_store.delete(name)
|
|
37
|
+
public_send(name)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
define_method("#{name}_loaded?") do
|
|
41
|
+
carddb_publisher_store.key?(name)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def carddb_load_publisher(name)
|
|
49
|
+
return carddb_publisher_store[name] if carddb_publisher_store.key?(name)
|
|
50
|
+
|
|
51
|
+
definition = self.class.carddb_publisher_definitions.fetch(name)
|
|
52
|
+
client = carddb_publisher_resolve_client(definition[:client])
|
|
53
|
+
slug = carddb_publisher_resolve_option(definition[:slug])
|
|
54
|
+
id = carddb_publisher_resolve_option(definition[:id])
|
|
55
|
+
|
|
56
|
+
raise ArgumentError, 'slug or id is required' if slug.blank? && id.blank?
|
|
57
|
+
|
|
58
|
+
record = client.publishers.fetch(id: id, slug: slug, cache: definition[:cache])
|
|
59
|
+
carddb_publisher_store[name] = record
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def carddb_publisher_store
|
|
63
|
+
@carddb_publisher_store ||= {}
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def carddb_publisher_resolve_client(option)
|
|
67
|
+
CardDB::Rails::Resolver.resolve_client(self, option)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def carddb_publisher_resolve_option(option)
|
|
71
|
+
CardDB::Rails::Resolver.resolve_option(self, option)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/concern'
|
|
4
|
+
require 'active_support/core_ext/object/blank'
|
|
5
|
+
require 'active_support/core_ext/string/inflections'
|
|
6
|
+
|
|
7
|
+
module CardDB
|
|
8
|
+
module Rails
|
|
9
|
+
module HasCardDBRecord
|
|
10
|
+
extend ActiveSupport::Concern
|
|
11
|
+
|
|
12
|
+
included do
|
|
13
|
+
class_attribute :carddb_record_definitions, instance_accessor: false, default: {}
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class_methods do
|
|
17
|
+
def has_carddb_record(
|
|
18
|
+
name = :carddb_record,
|
|
19
|
+
dataset_key:,
|
|
20
|
+
identifier:,
|
|
21
|
+
publisher_slug: nil,
|
|
22
|
+
game_key: nil,
|
|
23
|
+
client: nil,
|
|
24
|
+
cache: nil
|
|
25
|
+
)
|
|
26
|
+
name = name.to_sym
|
|
27
|
+
definition = {
|
|
28
|
+
dataset_key: dataset_key,
|
|
29
|
+
identifier: identifier,
|
|
30
|
+
publisher_slug: publisher_slug,
|
|
31
|
+
game_key: game_key,
|
|
32
|
+
client: client,
|
|
33
|
+
cache: cache
|
|
34
|
+
}.freeze
|
|
35
|
+
|
|
36
|
+
self.carddb_record_definitions = carddb_record_definitions.merge(name => definition)
|
|
37
|
+
|
|
38
|
+
define_method(name) do
|
|
39
|
+
carddb_load_record(name)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
define_method("#{name}!") do
|
|
43
|
+
public_send(name) || raise(CardDB::NotFoundError, "No CardDB record found for #{self.class.name}##{name}")
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
define_method("reload_#{name}") do
|
|
47
|
+
carddb_record_store.delete(name)
|
|
48
|
+
public_send(name)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
define_method("#{name}_loaded?") do
|
|
52
|
+
carddb_record_store.key?(name)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
unless name == :carddb_record
|
|
56
|
+
define_singleton_method("preload_#{name.to_s.pluralize}") do |records|
|
|
57
|
+
preload_carddb_records(records, as: name)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def preload_carddb_records(records, as: :carddb_record)
|
|
63
|
+
records = Array(records).compact
|
|
64
|
+
return records if records.empty?
|
|
65
|
+
|
|
66
|
+
definition = carddb_record_definitions.fetch(as.to_sym)
|
|
67
|
+
records.group_by { |record| record.send(:carddb_batch_group_key, definition) }.each_value do |group|
|
|
68
|
+
first_record = group.first
|
|
69
|
+
query_context = first_record.send(:carddb_query_context, definition)
|
|
70
|
+
identifiers = group.filter_map { |record| record.send(:carddb_resolve_option, definition[:identifier]) }.uniq
|
|
71
|
+
|
|
72
|
+
next if identifiers.empty?
|
|
73
|
+
|
|
74
|
+
results = query_context[:client].batch do |batch|
|
|
75
|
+
identifiers.each do |identifier|
|
|
76
|
+
batch.records.get(
|
|
77
|
+
publisher_slug: query_context[:publisher_slug],
|
|
78
|
+
game_key: query_context[:game_key],
|
|
79
|
+
dataset_key: query_context[:dataset_key],
|
|
80
|
+
identifier: identifier
|
|
81
|
+
)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
records_by_identifier = identifiers.zip(results).to_h
|
|
86
|
+
|
|
87
|
+
group.each do |record|
|
|
88
|
+
identifier = record.send(:carddb_resolve_option, definition[:identifier])
|
|
89
|
+
record.send(:carddb_write_record, as.to_sym, records_by_identifier[identifier])
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
records
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
private
|
|
98
|
+
|
|
99
|
+
def carddb_load_record(name)
|
|
100
|
+
return carddb_record_store[name] if carddb_record_store.key?(name)
|
|
101
|
+
|
|
102
|
+
definition = self.class.carddb_record_definitions.fetch(name)
|
|
103
|
+
identifier = carddb_resolve_option(definition[:identifier])
|
|
104
|
+
return carddb_write_record(name, nil) if identifier.blank?
|
|
105
|
+
|
|
106
|
+
context = carddb_query_context(definition)
|
|
107
|
+
record = context[:client].records.get(
|
|
108
|
+
publisher_slug: context[:publisher_slug],
|
|
109
|
+
game_key: context[:game_key],
|
|
110
|
+
dataset_key: context[:dataset_key],
|
|
111
|
+
identifier: identifier,
|
|
112
|
+
cache: definition[:cache]
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
carddb_write_record(name, record)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def carddb_write_record(name, record)
|
|
119
|
+
carddb_record_store[name] = record
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def carddb_record_store
|
|
123
|
+
@carddb_record_store ||= {}
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def carddb_batch_group_key(definition)
|
|
127
|
+
context = carddb_query_context(definition)
|
|
128
|
+
[
|
|
129
|
+
context[:client].object_id,
|
|
130
|
+
context[:publisher_slug],
|
|
131
|
+
context[:game_key],
|
|
132
|
+
context[:dataset_key]
|
|
133
|
+
]
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def carddb_query_context(definition)
|
|
137
|
+
client = carddb_resolve_client(definition[:client])
|
|
138
|
+
dataset_key = carddb_resolve_required_option(definition[:dataset_key], 'dataset_key')
|
|
139
|
+
publisher_slug = client.config.resolve_publisher(carddb_resolve_option(definition[:publisher_slug]))
|
|
140
|
+
game_key = client.config.resolve_game(carddb_resolve_option(definition[:game_key]))
|
|
141
|
+
|
|
142
|
+
raise ArgumentError, 'publisher_slug is required (no default configured)' if publisher_slug.blank?
|
|
143
|
+
raise ArgumentError, 'game_key is required (no default configured)' if game_key.blank?
|
|
144
|
+
|
|
145
|
+
{
|
|
146
|
+
client: client,
|
|
147
|
+
dataset_key: dataset_key,
|
|
148
|
+
publisher_slug: publisher_slug,
|
|
149
|
+
game_key: game_key
|
|
150
|
+
}
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def carddb_resolve_client(option)
|
|
154
|
+
CardDB::Rails::Resolver.resolve_client(self, option)
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def carddb_resolve_required_option(option, name)
|
|
158
|
+
CardDB::Rails::Resolver.resolve_required_option(self, option, name)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def carddb_resolve_option(option)
|
|
162
|
+
CardDB::Rails::Resolver.resolve_option(self, option)
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'rails/railtie'
|
|
4
|
+
require 'active_support/ordered_options'
|
|
5
|
+
|
|
6
|
+
module CardDB
|
|
7
|
+
module Rails
|
|
8
|
+
class Railtie < ::Rails::Railtie
|
|
9
|
+
config.carddb = ActiveSupport::OrderedOptions.new
|
|
10
|
+
config.carddb.credentials_key = :carddb
|
|
11
|
+
config.carddb.use_rails_cache = true
|
|
12
|
+
config.carddb.use_rails_logger = true
|
|
13
|
+
|
|
14
|
+
initializer 'carddb-rails.configure' do |app|
|
|
15
|
+
CardDB::Rails.configure do |config|
|
|
16
|
+
config.credentials_key = app.config.carddb.credentials_key
|
|
17
|
+
config.use_rails_cache = app.config.carddb.use_rails_cache
|
|
18
|
+
config.use_rails_logger = app.config.carddb.use_rails_logger
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
CardDB::Rails.apply_rails_configuration!(app)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
initializer 'carddb-rails.controller-helpers' do
|
|
25
|
+
ActiveSupport.on_load(:action_controller_base) do
|
|
26
|
+
include CardDB::Rails::ControllerHelpers
|
|
27
|
+
include CardDB::Rails::DeckControllerHelpers
|
|
28
|
+
|
|
29
|
+
helper_method :carddb_client
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|