Class: Cardio::Mod

Inherits:
Object
  • Object
show all
Defined in:
lib/cardio/mod.rb,
lib/cardio/mod/dirs.rb,
lib/cardio/mod/loader.rb,
lib/cardio/mod/modfile_api.rb,
lib/cardio/mod/load_strategy.rb,
lib/cardio/mod/modfile_loader.rb,
lib/cardio/mod/module_template.rb,
lib/cardio/mod/loader/set_loader.rb,
lib/cardio/mod/load_strategy/eval.rb,
lib/cardio/mod/loader/set_template.rb,
lib/cardio/mod/load_strategy/tmp_files.rb,
lib/cardio/mod/loader/set_pattern_loader.rb,
lib/cardio/mod/load_strategy/set_tmp_files.rb,
lib/cardio/mod/load_strategy/pattern_tmp_files.rb,
lib/cardio/mod/load_strategy/set_binding_magic.rb

Overview

A Card Mod (short for “module” or “modification”) is a discrete piece of Decko functionality. Mods are how the Decko community develops and shares code. If you want to customize a deck in a way that can’t be done on the site itself, try a mod.

The simplest way to add a mod is to run this command in your deck:

decko generate card:mod MOD_NAME

This will create the following directories:

DECK_NAME/mod/MOD_NAME
DECK_NAME/mod/MOD_NAME/lib
DECK_NAME/mod/MOD_NAME/public
DECK_NAME/mod/MOD_NAME/set

The lib directory contains libraries, of course. And files in the public directory are public and served directly.

But in most mods, the focal point is the set directory.

## Set Modules

Set modules define methods for a given set of cards and their format objects. They are defined in a mod’s set directory. For example, suppose you’ve created a mod that called biz, your deck has Company cards, and you want to extend the behavior of those cards.

You can add a set module like so:

decko generate set biz type company

This will create the following two files:

mod/biz/set/type/company.rb
mod/biz/spec/set/type/company.rb

If you would like to break this code into smaller files, you can extend this pattern into another directory, eg:

mod/biz/set/type/company/foo.rb
mod/biz/set/type/company/bar.rb

The general pattern can be expressed as follows:

DECKNAME/mod/MODNAME/set/SET_PATTERN/ANCHOR[/FREENAME].rb

Learn more:

- {Card} introduces card objects
- {Card::Set} provides an overview of how set modules work
- {Card::Set::Format} explains the basics of the format API
- {Card::Set::Format::AbstractFormat} explains the basics of the view definition API
- {Card::Set::Event::Api} explains the basics of the event API

## Other Directories

Other ways your mod can extend Decko functionality include:

- **set_pattern** for additional {Card::Set::Pattern set patterns},
  or types of sets.
- **file** for fixed initial card content

Defined Under Namespace

Modules: ModfileApi Classes: BindingMagic, Dirs, LoadStrategy, Loader, ModfileLoader, ModuleTemplate

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path, index) ⇒ Mod

Returns a new instance of Mod.



65
66
67
68
69
# File 'lib/cardio/mod.rb', line 65

def initialize name, path, index
  @name = Mod.normalize_name name
  @path = path
  @index = index
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



63
64
65
# File 'lib/cardio/mod.rb', line 63

def index
  @index
end

#nameObject (readonly)

Returns the value of attribute name.



63
64
65
# File 'lib/cardio/mod.rb', line 63

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



63
64
65
# File 'lib/cardio/mod.rb', line 63

def path
  @path
end

Class Method Details

.dependencies(name, nickname = true) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/cardio/mod.rb', line 128

def dependencies name, nickname=true
  return unless (spec = gem_spec name, nickname)

  deps = spec&.dependencies || []
  dep_names = deps.map { |dep| dependencies dep.name, false }
  (dep_names << spec).flatten.compact.uniq
end

.dirsObject

Returns an array of Rails::Path objects.

Returns:

  • an array of Rails::Path objects



124
125
126
# File 'lib/cardio/mod.rb', line 124

def dirs
  @dirs ||= Mod::Dirs.new(Cardio.paths["mod"].existent)
end

.each_path(&block) ⇒ Object



136
137
138
139
# File 'lib/cardio/mod.rb', line 136

def each_path &block
  each_simple_path(&block)
  each_gem_path(&block)
end

.gem_specsHash

Returns in the form{ modname(String) => Gem::Specification }.

Returns:

  • (Hash)

    in the form{ modname(String) => Gem::Specification }



142
143
144
145
146
# File 'lib/cardio/mod.rb', line 142

def gem_specs
  Bundler.definition.specs.each_with_object({}) do |gem_spec, h|
    h[gem_spec.name] = gem_spec if gem_spec? gem_spec
  end
end

.loadObject



113
114
115
116
117
118
119
120
121
# File 'lib/cardio/mod.rb', line 113

def load
  return if ENV["CARD_MODS"] == "none"

  if Card.take
    Loader.load_mods
  else
    Rails.logger.warn "empty database"
  end
end

.normalize_name(name) ⇒ Object



148
149
150
# File 'lib/cardio/mod.rb', line 148

def normalize_name name
  name.to_s.sub(/^card-mod-/, "")
end

Instance Method Details

#assets_pathObject



88
89
90
# File 'lib/cardio/mod.rb', line 88

def assets_path
  File.join @path, "assets"
end

#codenameObject



75
76
77
# File 'lib/cardio/mod.rb', line 75

def codename
  "mod_#{name}"
end

#ensure_mod_installedObject



92
93
94
95
96
97
98
# File 'lib/cardio/mod.rb', line 92

def ensure_mod_installed
  Card::Auth.as_bot do
    card = ensure_mod_card
    card.ensure_mod_script_card
    card.ensure_mod_style_card
  end
end

#mod_card_nameObject



71
72
73
# File 'lib/cardio/mod.rb', line 71

def mod_card_name
  "mod: #{name}"
end

#public_pathObject



84
85
86
# File 'lib/cardio/mod.rb', line 84

def public_path
  File.join @path, "public"
end

#tmp_dir(type) ⇒ Object



79
80
81
82
# File 'lib/cardio/mod.rb', line 79

def tmp_dir type
  File.join Cardio.paths["tmp/#{type}"].first,
            "mod#{'%03d' % (@index + 1)}-#{@name}"
end