card 1.103.3 → 1.104.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/config/environments/development.rb +4 -2
  4. data/config/environments/test.rb +1 -1
  5. data/config/initializers/01_core_extensions/array.rb +4 -1
  6. data/config/initializers/01_core_extensions/object.rb +1 -1
  7. data/config/initializers/02_patches/active_record.rb +16 -17
  8. data/config/locales/de.yml +8 -564
  9. data/config/locales/es.yml +2 -3
  10. data/db/migrate/20110511221913_require_earlier_migrations.rb +1 -1
  11. data/db/migrate/20120105203350_require_1_8_migrations.rb +1 -1
  12. data/db/migrate/20121111025347_require_1_10_migrations.rb +1 -1
  13. data/db/migrate/20211128040849_virtuals_updated_at.rb +11 -0
  14. data/db/migrate_core_cards/20140307231621_user_data_to_cards.rb +0 -1
  15. data/db/migrate_core_cards/20190502130029_add_shark_and_help_desk_role.rb +0 -1
  16. data/db/schema.rb +19 -16
  17. data/db/seed/new/card_actions.yml +1323 -2091
  18. data/db/seed/new/card_acts.yml +2 -2
  19. data/db/seed/new/card_references.yml +629 -741
  20. data/db/seed/new/cards.yml +2593 -5557
  21. data/db/seed/new/schema_migrations.yml +2 -0
  22. data/db/seed/new/schema_migrations_core_cards.yml +8 -0
  23. data/db/seed/test/fixtures/card_actions.yml +4304 -5144
  24. data/db/seed/test/fixtures/card_acts.yml +780 -840
  25. data/db/seed/test/fixtures/card_changes.yml +110 -110
  26. data/db/seed/test/fixtures/card_references.yml +2908 -2992
  27. data/db/seed/test/fixtures/cards.yml +8781 -11961
  28. data/db/seed/test/fixtures/schema_migrations.yml +2 -0
  29. data/db/seed/test/fixtures/schema_migrations_core_cards.yml +8 -0
  30. data/db/test_seed.rb +2 -5
  31. data/db/version.txt +1 -1
  32. data/db/version_core_cards.txt +1 -1
  33. data/lib/card/auth/current.rb +1 -1
  34. data/lib/card/auth/permissions.rb +37 -35
  35. data/lib/card/content/all.rb +3 -3
  36. data/lib/card/content/chunk.rb +1 -0
  37. data/lib/card/director/act_direction.rb +1 -3
  38. data/lib/card/director/card_methods.rb +0 -1
  39. data/lib/card/director/phases.rb +1 -0
  40. data/lib/card/director.rb +1 -0
  41. data/lib/card/env/location.rb +8 -8
  42. data/lib/card/env/serializable.rb +33 -0
  43. data/lib/card/env/serialization.rb +14 -6
  44. data/lib/card/env/slot_options.rb +1 -1
  45. data/lib/card/env/support.rb +30 -0
  46. data/lib/card/env.rb +13 -68
  47. data/lib/card/fetch/all.rb +3 -3
  48. data/lib/card/fetch/card_class.rb +5 -11
  49. data/lib/card/fetch/results.rb +2 -2
  50. data/lib/card/format/error.rb +2 -2
  51. data/lib/card/format.rb +1 -1
  52. data/lib/card/model/save_helper/save_arguments.rb +2 -2
  53. data/lib/card/model/save_helper.rb +2 -2
  54. data/lib/card/name/all/descendants.rb +9 -7
  55. data/lib/card/name/all/parts.rb +1 -1
  56. data/lib/card/name/all.rb +4 -3
  57. data/lib/card/name/card_class.rb +1 -0
  58. data/lib/card/name/fields_and_traits.rb +6 -30
  59. data/lib/card/name/name_variants.rb +5 -1
  60. data/lib/card/name.rb +0 -8
  61. data/lib/card/query/abstract_query/tie.rb +2 -3
  62. data/lib/card/query/card_query/normalization.rb +1 -1
  63. data/lib/card/query/sql_statement/order.rb +5 -6
  64. data/lib/card/query/value.rb +10 -7
  65. data/lib/card/reference/all.rb +9 -7
  66. data/lib/card/reference.rb +36 -41
  67. data/lib/card/rule/all.rb +3 -3
  68. data/lib/card/set/advanced_api.rb +5 -0
  69. data/lib/card/set/event/delayed_event.rb +8 -1
  70. data/lib/card/set/event.rb +1 -0
  71. data/lib/card/set/helpers.rb +30 -17
  72. data/lib/card/set/pattern/all.rb +13 -4
  73. data/lib/card/set/pattern/base.rb +12 -18
  74. data/lib/card/set/pattern/class_methods.rb +13 -13
  75. data/lib/card/set/pattern.rb +30 -19
  76. data/lib/card/set/trait.rb +16 -1
  77. data/lib/card/set/type.rb +3 -0
  78. data/lib/card/subcards/add.rb +1 -3
  79. data/lib/card/subcards/all.rb +30 -56
  80. data/lib/card/view/options.rb +7 -5
  81. data/lib/card/view/permission.rb +4 -11
  82. data/lib/cardio/cli.rb +1 -0
  83. data/lib/cardio/commands/custom.rb +60 -0
  84. data/lib/cardio/commands/rake_command/parser.rb +49 -48
  85. data/lib/cardio/commands/rake_command.rb +17 -15
  86. data/lib/cardio/commands/rspec_command.rb +2 -0
  87. data/lib/cardio/commands.rb +69 -69
  88. data/lib/cardio/generators.rb +56 -3
  89. data/lib/cardio/migration/deck.rb +0 -0
  90. data/lib/cardio/migration/deck_structure.rb +2 -0
  91. data/lib/cardio/migration/import/import_data/card_content.rb +1 -1
  92. data/lib/cardio/migration/import.rb +2 -2
  93. data/lib/cardio/migration.rb +26 -2
  94. data/lib/cardio/mod/class_methods.rb +112 -0
  95. data/lib/cardio/mod/dirs.rb +15 -11
  96. data/lib/cardio/mod/eat/edibles.rb +92 -0
  97. data/lib/cardio/mod/eat.rb +81 -0
  98. data/lib/cardio/mod/load_strategy/tmp_files.rb +1 -1
  99. data/lib/cardio/mod/modfile_api.rb +5 -0
  100. data/lib/cardio/mod/poop.rb +135 -0
  101. data/lib/cardio/mod.rb +20 -80
  102. data/lib/cardio/railtie.rb +20 -8
  103. data/lib/cardio/schema.rb +11 -10
  104. data/lib/cardio/version.rb +35 -0
  105. data/lib/cardio.rb +12 -0
  106. data/lib/generators/deck/deck_generator.rb +3 -2
  107. data/lib/generators/deck/templates/Gemfile.erb +0 -4
  108. data/lib/generators/deck/templates/config/application.rb.erb +74 -55
  109. data/lib/generators/deck/templates/rspec.erb +1 -1
  110. data/lib/generators/deck/templates/spec/javascripts/support/decko_jasmine.yml.erb +0 -1
  111. data/lib/generators/mod/USAGE +1 -0
  112. data/lib/tasks/card/migrate.rake +41 -1
  113. data/lib/tasks/card/mod.rake +15 -8
  114. data/lib/tasks/card.rake +47 -87
  115. data/mod/admin/locales/de.yml +4 -0
  116. data/mod/admin/set/self/admin.rb +10 -8
  117. data/mod/core/data/production.yml +7 -0
  118. data/mod/core/data/test.yml +30 -0
  119. data/mod/core/locales/de.yml +28 -0
  120. data/mod/core/set/all/assign_attributes.rb +1 -33
  121. data/mod/core/set/all/content.rb +3 -1
  122. data/mod/core/set/all/initialize.rb +1 -4
  123. data/mod/core/set/all/name_events.rb +3 -18
  124. data/mod/core/set/all/reference_events.rb +29 -28
  125. data/mod/core/set/all/subcards.rb +6 -2
  126. data/mod/core/set/all/trash.rb +2 -3
  127. data/mod/core/set/all/type.rb +67 -18
  128. data/mod/core/set/self/version.rb +1 -1
  129. data/mod/core/spec/set/self/trash_spec.rb +1 -1
  130. data/mod/standard/{file → data/files}/favicon/image-icon.png +0 -0
  131. data/mod/standard/{file → data/files}/favicon/image-large.png +0 -0
  132. data/mod/standard/{file → data/files}/favicon/image-medium.png +0 -0
  133. data/mod/standard/{file → data/files}/favicon/image-original.png +0 -0
  134. data/mod/standard/{file → data/files}/favicon/image-small.png +0 -0
  135. data/mod/standard/{file → data/files}/logo/image-original.svg +0 -0
  136. metadata +39 -29
  137. data/db/migrate_core_cards/20150605115802_add_performance_log_card.rb +0 -7
  138. data/lib/card/env/request_assignments.rb +0 -24
  139. data/lib/card/set/code_nest.rb +0 -15
  140. data/lib/card/version.rb +0 -11
  141. data/lib/cardio/commands/USAGE +0 -28
  142. data/lib/cardio/generators/class_methods.rb +0 -35
  143. data/lib/tasks/card/asset.rake +0 -22
  144. data/mod/core/spec/set/all/clean_me_spec.rb +0 -258
  145. data/mod/core/spec/set/all/export_spec.rb +0 -71
  146. data/tmpsets/set_pattern/100-all.rb +0 -22
  147. data/tmpsets/set_pattern/101-all_plus.rb +0 -24
  148. data/tmpsets/set_pattern/102-type.rb +0 -40
  149. data/tmpsets/set_pattern/103-star.rb +0 -26
  150. data/tmpsets/set_pattern/104-rstar.rb +0 -28
  151. data/tmpsets/set_pattern/105-rule.rb +0 -28
  152. data/tmpsets/set_pattern/106-right.rb +0 -35
  153. data/tmpsets/set_pattern/107-type_plus_right.rb +0 -43
  154. data/tmpsets/set_pattern/108-self.rb +0 -34
@@ -1,53 +1,59 @@
1
1
  require "English"
2
+ require "colorize"
3
+ require "cardio/commands/custom"
2
4
 
3
5
  module Cardio
4
- # manage different types of commands that can be run via bin/card and bin/decko
6
+ # manage different types of commands that can be run via bin/card (and bin/decko)
5
7
  class Commands
8
+ include Custom
9
+
6
10
  attr_reader :command, :args
7
11
 
8
- # class methods for commands
9
- # (basically simulates cattr_reader, which isn't available here)
10
- module Accessors
11
- def aliases
12
- @aliases ||= {
13
- "rs" => "rspec",
14
- "jm" => "jasmine",
15
- "g" => "generate",
16
- "d" => "destroy",
17
- "c" => "console",
18
- "db" => "dbconsole",
19
- "r" => "runner",
20
- "v" => "version",
21
- "h" => "help"
22
- }
23
- end
12
+ class << self
13
+ attr_accessor :current
24
14
 
25
- def commands
26
- @commands ||= {
27
- rails: %w[generate destroy plugin benchmarker profiler
28
- console dbconsole application runner],
29
- rake: %w[seed reseed load update],
30
- custom: %w[new rspec jasmine version help]
31
- }
15
+ def gem
16
+ current&.gem
32
17
  end
33
18
  end
34
19
 
35
- extend Accessors
20
+ def map
21
+ @map ||= {
22
+ new: { desc: "create a new deck", group: :shark, via: :call },
23
+ seed: { desc: "populate a database", group: :shark, via: :rake },
24
+ update: { desc: "run data updates", group: :shark, alias: :u, via: :rake },
25
+ version: { desc: "#{gem} gem version", group: :shark, alias: :v, via: :call },
26
+ help: { desc: "show this text", group: :shark, alias: :h, via: :call},
27
+
28
+ console: { desc: "start a ruby console", group: :monkey, alias: :c },
29
+ dbconsole: { desc: "start a database console", group: :monkey, alias: :db },
30
+ runner: { desc: "run code in app environment", group: :monkey, alias: :r },
31
+ rspec: { desc: "run rspec tests", group: :monkey, alias: :rs, via: :call },
32
+ generate: { desc: "generate templated code", group: :monkey, alias: :g },
33
+ poop: { desc: "export card data to mod yaml", group: :monkey, via: :rake },
34
+ eat: { desc: "ingest card data from mod yaml", group: :monkey, via: :rake }
35
+ }
36
+ end
37
+
38
+ # TODO: review the following. see if any work well enough to include
39
+ #
40
+ # application Generate the Rails application code
41
+ # destroy Undo code generated with "generate" (short-cut alias: "d")
42
+ # benchmarker See how fast a piece of code runs
43
+ # profiler Get profile information from a piece of code
44
+ # plugin Install a plugin
45
+ # jasmine
36
46
 
37
47
  def initialize args
38
48
  @args = args
39
- @command = self.class.aliases[args.first] || args.first
49
+ @command = command_for_key args.first&.to_sym
40
50
  ENV["PRY_RESCUE_RAILS"] = "1" if rescue?
41
51
  @args.shift unless handler == :rails
52
+ Commands.current = self
42
53
  end
43
54
 
44
- def rescue?
45
- args.delete "--rescue"
46
- end
47
-
48
- def handler
49
- commands = self.class.commands
50
- @handler ||= commands.keys.find { |k| commands[k].include? command }
55
+ def gem
56
+ "card"
51
57
  end
52
58
 
53
59
  def run
@@ -56,65 +62,59 @@ module Cardio
56
62
  run_rails
57
63
  when :rake
58
64
  run_rake
59
- when :custom
65
+ when :call
60
66
  send "run_#{command}"
61
- else
62
- unrecognized
67
+ when :unknown
68
+ unknown_error
63
69
  end
64
70
  exit 0
65
71
  end
66
72
 
67
- # runs all commands in "rails" list
68
- def run_rails
69
- require "rails/commands"
70
- end
73
+ private
71
74
 
72
- # runs all commands in "rake" list
73
- def run_rake
74
- require "cardio/commands/rake_command"
75
- RakeCommand.new("#{rake_prefix}:#{command}", args).run
76
- end
75
+ def command_for_key key
76
+ return :help unless key
77
+ return key if map.key? key
77
78
 
78
- def rake_prefix
79
- "card"
79
+ map.each { |k, v| return k if v[:alias] == key }
80
+ @unknown = true
81
+ key
80
82
  end
81
83
 
82
- # ~~~~~~~~~ CUSTOM COMMANDS ~~~~~~~~~~~~ #
84
+ def rescue?
85
+ args.delete "--rescue"
86
+ end
83
87
 
84
- def run_help
85
- puts File.read(File.expand_path("../commands/USAGE", __FILE__))
88
+ def config
89
+ map[command]
86
90
  end
87
91
 
88
- def run_new
89
- if ["-h", "--help"].include? args.first
90
- require "cardio/commands/application"
91
- else
92
- puts "Can't initialize a new deck within the directory of another, " \
93
- "please change to a non-deck directory first.\n"
94
- puts "Type 'decko' for help."
95
- exit 1
96
- end
92
+ def handler
93
+ @handler ||= @unknown ? :unknown : (config[:via] || :rails)
97
94
  end
98
95
 
99
- def run_version
100
- require "card/version"
101
- puts "Decko #{Card::Version.release}"
96
+ # runs all commands in "rails" list
97
+ def run_rails
98
+ require generator_requirement if command == :generate
99
+ require "rails/commands"
102
100
  end
103
101
 
104
- def run_rspec
105
- require "cardio/commands/rspec_command"
106
- RspecCommand.new(args).run
102
+ def generator_requirement
103
+ "cardio/generators"
107
104
  end
108
105
 
109
- def run_jasmine
106
+ # runs all commands in "rake" list
107
+ def run_rake
110
108
  require "cardio/commands/rake_command"
111
- RakeCommand.new("spec:javascript", envs: "test").run
109
+ RakeCommand.new(gem, command, args).run
112
110
  end
113
111
 
114
112
  # ~~~~~~~~~~~~~~~~~~~~~ catch-all -------------- #
115
113
 
116
- def unrecognized
117
- puts "Error: Command not recognized: #{command}"
114
+ def unknown_error
115
+ puts "----------------------------------------------\n" \
116
+ "ERROR: Command not recognized: #{command}\n" \
117
+ "----------------------------------------------\n".red
118
118
  run_help
119
119
  exit 1
120
120
  end
@@ -2,19 +2,72 @@
2
2
 
3
3
  require "rails/generators"
4
4
  require "rails/generators/active_record"
5
+ require "colorize"
5
6
 
6
7
  module Cardio
7
8
  # for now, this just fulfills zeitwerk expectations. File is here for require calls.
8
9
  module Generators
9
- # noop
10
+ # methods shared across Generator bases (which inherit from Rails generator classes)
11
+ module ClassMethods
12
+ def source_root path=nil
13
+ if path
14
+ @_card_source_root = path
15
+ else
16
+ @_card_source_root ||= File.expand_path(
17
+ "../../generators/#{generator_name}/templates", __FILE__
18
+ )
19
+ end
20
+ end
21
+
22
+ # Override Rails default banner (using card/decko for the command name).
23
+ def banner
24
+ usage_args = arguments.map(&:usage).join " "
25
+ text = "\n#{banner_command} generate #{namespace} #{usage_args} [options]".green
26
+ text.gsub(/\s+/, " ")
27
+ end
28
+
29
+ def banner_command
30
+ Commands.gem
31
+ end
32
+
33
+ # Override Rails namespace handling so we can put generators in `module Cardio`
34
+ def namespace name=nil
35
+ return super if name
36
+ @namespace ||= super.sub(/cardio:/, "")
37
+ end
38
+ end
39
+ delegate :banner_command, to: :class
10
40
  end
11
41
  end
12
42
 
13
43
  module Rails
14
44
  # override to hide all the rails generators that don't apply in a card/decko context
15
45
  module Generators
16
- def self.sorted_groups
17
- [["card", %w[mod set migration]]]
46
+ class << self
47
+ # TODO: autogenerate
48
+ def generator_names
49
+ %i[mod set migration]
50
+ end
51
+
52
+ def help command="generate"
53
+ caller = Cardio::Commands.gem
54
+ puts "Usage:"
55
+ puts " #{caller} #{command} GENERATOR [args] [options]".green
56
+ puts
57
+ puts "General options:"
58
+ puts " -h, [--help] # Print generator's options and usage"
59
+ puts " -p, [--pretend] # Run but do not make any changes"
60
+ puts " -f, [--force] # Overwrite files that already exist"
61
+ puts " -s, [--skip] # Skip files that already exist"
62
+ puts " -q, [--quiet] # Suppress status output"
63
+ puts
64
+ puts "Please choose a generator below."
65
+ puts
66
+ generator_names.each do |name|
67
+ puts " #{name}".light_cyan
68
+ end
69
+ puts
70
+ end
18
71
  end
19
72
  end
20
73
  end
File without changes
@@ -1,5 +1,7 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
 
3
+ # ENV["NO_CARD_LOAD"] = "true"
4
+
3
5
  require "cardio/migration"
4
6
 
5
7
  module Cardio
@@ -5,7 +5,7 @@ module Cardio
5
5
  # handles card content for import
6
6
  module CardContent
7
7
  def card_content data
8
- File.read(content_path(data))
8
+ File.read content_path(data)
9
9
  end
10
10
 
11
11
  def content_changed? data
@@ -64,11 +64,11 @@ module Cardio
64
64
  private
65
65
 
66
66
  def update &block
67
- ImportData.update(@data_path, &block)
67
+ ImportData.update @data_path, &block
68
68
  end
69
69
 
70
70
  def importer
71
- @importer ||= ImportData.new(@data_path)
71
+ @importer ||= ImportData.new @data_path
72
72
  end
73
73
 
74
74
  # Returns an array of hashes with card attributes
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Cardio
4
4
  class Migration < ActiveRecord::Migration[4.2]
5
- include Card::Model::SaveHelper
5
+ include Card::Model::SaveHelper unless ENV["NO_CARD_LOAD"]
6
6
  @type = :deck_cards
7
7
 
8
8
  class << self
@@ -43,12 +43,36 @@ module Cardio
43
43
  Schema.assume_migrated_upto_version type
44
44
  end
45
45
 
46
+ def assume_current
47
+ migration_context do |mc|
48
+ versions = mc.migrations.map(&:version)
49
+ migrated = mc.get_all_versions
50
+ mark_as_migrated versions - migrated
51
+ end
52
+ end
53
+
46
54
  def data_path filename=nil
47
55
  File.join([migration_paths.first, "data", filename].compact)
48
56
  end
57
+
58
+ private
59
+
60
+ def mark_as_migrated versions
61
+ sql = connection.send :insert_versions_sql, versions
62
+ connection.execute sql
63
+ end
64
+
65
+ def connection
66
+ ActiveRecord::Base.connection
67
+ end
68
+
69
+ def migration_context &block
70
+ Schema.migration_context type, &block
71
+ end
49
72
  end
50
73
 
51
74
  def contentedly
75
+ return yield if ENV["NO_CARD_LOAD"]
52
76
  Card::Cache.reset_all
53
77
  Schema.mode "" do
54
78
  Card::Auth.as_bot do
@@ -111,7 +135,7 @@ module Cardio
111
135
  end
112
136
 
113
137
  # Execute this migration in the named direction
114
- # copied from ActiveRecord to wrap 'up' in 'contentendly'
138
+ # copied from ActiveRecord to wrap 'up' in 'contentedly'
115
139
  def exec_migration conn, direction
116
140
  @connection = conn
117
141
  if respond_to?(:change)
@@ -0,0 +1,112 @@
1
+ module Cardio
2
+ class Mod
3
+ # class methods for Cardio::Mod
4
+ module ClassMethods
5
+ def load
6
+ return if ENV["CARD_MODS"] == "none"
7
+
8
+ if Card.take
9
+ Loader.load_mods
10
+ else
11
+ Rails.logger.warn "empty database"
12
+ end
13
+ end
14
+
15
+ # @return an array of Rails::Path objects
16
+ def dirs
17
+ @dirs ||= Mod::Dirs.new Cardio.paths["mod"].existent
18
+ end
19
+
20
+ def fetch mod_name
21
+ dirs.fetch_mod mod_name
22
+ end
23
+
24
+ def normalize_name name
25
+ name.to_s.sub(/^card-mod-/, "")
26
+ end
27
+
28
+ def missing
29
+ Card.search(type: :mod).reject { |mod_card| fetch mod_card.modname }
30
+ end
31
+
32
+ def ensure_uninstalled
33
+ missing.each do |mod_card|
34
+ Card::Auth.as_bot do
35
+ Card[:all, :style].drop_item mod_card
36
+ delete_auto_installed_cards mod_card
37
+ end
38
+ end
39
+ end
40
+
41
+ def ensure_installed
42
+ Card::Cache.reset_all
43
+ puts "installing card mods".green
44
+ Cardio.mods.each(&:ensure)
45
+ Card::Assets.refresh_assets force: true
46
+ end
47
+
48
+ def dependencies name, nickname=true
49
+ return unless (spec = gem_spec name, nickname)
50
+
51
+ deps = spec&.dependencies || []
52
+ dep_names = deps.map { |dep| dependencies dep.name, false }
53
+ (dep_names << spec).flatten.compact.uniq
54
+ end
55
+
56
+ def each_path &block
57
+ each_simple_path(&block)
58
+ each_gem_path(&block)
59
+ end
60
+
61
+ # @return [Hash] in the form{ modname(String) => Gem::Specification }
62
+ def gem_specs
63
+ Bundler.definition.specs.each_with_object({}) do |gem_spec, h|
64
+ h[gem_spec.name] = gem_spec if gem_spec? gem_spec
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ # it would be nice if this were easier...
71
+ def delete_auto_installed_cards mod_card
72
+ auto_installed_cards(mod_card).each do |card|
73
+ card.codename = nil
74
+ card.delete!
75
+ end
76
+ end
77
+
78
+ def auto_installed_cards mod_card
79
+ [mod_card].tap do |cards|
80
+ mod_card.each_descendant do |card|
81
+ cards.unshift card
82
+ end
83
+ end
84
+ end
85
+
86
+ def gem_spec name, nickname=true
87
+ name = "card-mod-#{name}" if nickname && !name.match?(/^card-mod/)
88
+ spec = Gem::Specification.stubs_for(name)&.first
89
+ gem_spec?(spec) ? spec : nil
90
+ end
91
+
92
+ def each_simple_path &block
93
+ Cardio.paths["mod"].each do |mods_path|
94
+ Dir.glob("#{mods_path}/*").each(&block)
95
+ end
96
+ end
97
+
98
+ def each_gem_path
99
+ gem_specs.each_value do |spec|
100
+ yield spec.full_gem_path
101
+ end
102
+ end
103
+
104
+ # @return [True/False]
105
+ def gem_spec? spec
106
+ return unless spec
107
+
108
+ spec.name.match?(/^card-mod-/) || spec.metadata["card-mod"].present?
109
+ end
110
+ end
111
+ end
112
+ end
@@ -97,6 +97,13 @@ module Cardio
97
97
  fetch_mod(mod_name)&.path
98
98
  end
99
99
 
100
+ def subpaths *subdirs
101
+ @mods.each_with_object({}) do |mod, hash|
102
+ path = mod.subpath(*subdirs)
103
+ hash[mod.name] = path if path
104
+ end
105
+ end
106
+
100
107
  def fetch_mod mod_name
101
108
  @mods_by_name[Mod.normalize_name(mod_name)]
102
109
  end
@@ -129,12 +136,15 @@ module Cardio
129
136
  end
130
137
  end
131
138
 
132
- def each_public_path
133
- @mods.each do |mod|
134
- path = mod.public_path
135
- next unless Dir.exist? path
139
+ def each_subpath *subdirs
140
+ subpaths(*subdirs).each do |mod_name, subpath|
141
+ yield mod_name, subpath
142
+ end
143
+ end
136
144
 
137
- yield mod.name, path
145
+ def load_from_gemfile
146
+ Cardio::Mod.gem_specs.each do |mod_name, mod_spec|
147
+ add_gem_mod mod_name, mod_spec.full_gem_path
138
148
  end
139
149
  end
140
150
 
@@ -154,12 +164,6 @@ module Cardio
154
164
  add_mod filename unless filename.match?(/^\./)
155
165
  end
156
166
  end
157
-
158
- def load_from_gemfile
159
- Cardio::Mod.gem_specs.each do |mod_name, mod_spec|
160
- add_gem_mod mod_name, mod_spec.full_gem_path
161
- end
162
- end
163
167
  end
164
168
  end
165
169
  end
@@ -0,0 +1,92 @@
1
+ module Cardio
2
+ class Mod
3
+ class Eat
4
+ # item handling for Mod::Eat (importables)
5
+ module Edibles
6
+ # list of card attribute hashes
7
+ # @return [Array <Hash>]
8
+ def edibles
9
+ mods_with_data.map { |mod| mod_edibles mod }.flatten
10
+ end
11
+
12
+ private
13
+
14
+ # if mod is specified, consider only that mod
15
+ # @return [Array <Cardio::Mod>]
16
+ def mods_with_data
17
+ paths = Mod.dirs.subpaths "data"
18
+ mod_names = @mod ? ensure_mod_data_path(paths) : paths.keys
19
+ mod_names.map { |mod_name| Mod.fetch mod_name }
20
+ end
21
+
22
+ def ensure_mod_data_path paths
23
+ return [@mod] if paths[@mod]
24
+
25
+ raise "no data directory found for mod #{@mod}".red
26
+ end
27
+
28
+ # @return [Array <Hash>]
29
+ def mod_edibles mod
30
+ environments.map { |env| items_for_environment mod, env }.compact
31
+ end
32
+
33
+ def items_for_environment mod, env
34
+ return unless (items = items_from_yaml mod, env)
35
+
36
+ items = items.map do |item|
37
+ item.is_a?(String) ? items_from_yaml(mod, env, item) : item
38
+ end.flatten.compact
39
+ interpret_items mod, items
40
+ end
41
+
42
+ def interpret_items mod, items
43
+ each_card_hash(items) { |hash| handle_attachments mod, hash }
44
+ end
45
+
46
+ def items_from_yaml mod, env, filename=nil
47
+ source = "#{env}#{'/' if filename.present?}#{filename}.yml"
48
+ return unless (path = mod.subpath "data", source)
49
+
50
+ YAML.load_file path
51
+ end
52
+
53
+ # for processing that needs to happen on all cards, including fields
54
+ def each_card_hash items
55
+ items.each do |item|
56
+ yield item
57
+ item[:subfields]&.values&.each { |val| yield val if val.is_a? Hash }
58
+ end
59
+ items
60
+ end
61
+
62
+ def handle_attachments mod, hash
63
+ each_attachment hash do |key, filename|
64
+ hash[key] = mod_file mod, filename
65
+ hash[:mod] = mod.name if hash[:storage_type] == :coded
66
+ end
67
+ end
68
+
69
+ def each_attachment hash
70
+ attachment_keys.each { |key| yield key, hash[key] if hash.key? key }
71
+ end
72
+
73
+ def mod_file mod, filename
74
+ File.open mod.subpath("data/files", filename)
75
+ end
76
+
77
+ def attachment_keys
78
+ @attachment_keys ||= Card.uploaders.keys
79
+ end
80
+
81
+ # @return [Array <Symbol>]
82
+ # holarchical. each includes the previous
83
+ # production = [:production],
84
+ # development = [:production, :development], etc.
85
+ def environments
86
+ index = DATA_ENVIRONMENTS.index(@env&.to_sym || Rails.env.to_sym) || -1
87
+ DATA_ENVIRONMENTS[0..index]
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,81 @@
1
+ require "timecop"
2
+
3
+ require "pry"
4
+
5
+ DATA_ENVIRONMENTS = %i[production development test].freeze
6
+ ENV["STORE_CODED_FILES"] = "true"
7
+
8
+ module Cardio
9
+ class Mod
10
+ # import data from data directory of mods
11
+ # (list of card attributes)
12
+ # https://docs.google.com/document/d/13K_ynFwfpHwc3t5gnLeAkZJZHco1wK063nJNYwU8qfc/edit#
13
+ class Eat
14
+ include Card::Model::SaveHelper
15
+ include Edibles
16
+
17
+ def initialize mod: nil, env: nil, user: nil, verbose: nil
18
+ @mod = mod
19
+ @env = env
20
+ @user_id = user&.card_id
21
+ @verbose = !verbose.nil?
22
+ end
23
+
24
+ def up
25
+ Card::Cache.reset_all
26
+ Card::Mailer.perform_deliveries = false
27
+ Card::Auth.as_bot do
28
+ edibles.each do |edible|
29
+ track edible do
30
+ current_user edible.delete(:user)
31
+ time_machine edible.delete(:time) do
32
+ ensure_card edible
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def track edible
42
+ rescuing edible do
43
+ # puts "eat: #{edible}" if @verbose
44
+ card = yield
45
+ puts "eaten: #{card.name}".green # if @verbose
46
+ end
47
+ end
48
+
49
+ def rescuing edible
50
+ yield
51
+ rescue StandardError => e
52
+ # binding.pry
53
+ puts edible
54
+ puts e.message.red
55
+ puts e.backtrace.join("\n") if @verbose
56
+ end
57
+
58
+ def current_user item_user
59
+ Card::Auth.current_id = item_user&.card_id || @user_id || Card::WagnBotID
60
+ end
61
+
62
+ def time_machine value, &block
63
+ return yield unless value.present?
64
+
65
+ Timecop.freeze Time.at(time_integer(value)), &block
66
+ end
67
+
68
+ def time_integer value
69
+ case value
70
+ when /^[+-]\d+$/
71
+ # plus or minus an integer (safe to eval)
72
+ eval "#{Time.now.to_i} #{value}", binding, __FILE__, __LINE__
73
+ when Integer
74
+ value
75
+ else
76
+ raise TypeError, "invalid time value: #{value}. accepts int, +int, and -int"
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end