Module: Card::Set

Extended by:
I18nScope, Registrar
Includes:
AdvancedApi, Event::Api, Format, Helpers, Inheritance, Trait
Defined in:
lib/card/set.rb,
lib/card/set/type.rb,
lib/card/set/event.rb,
lib/card/set/trait.rb,
lib/card/set/format.rb,
lib/card/set/helpers.rb,
lib/card/set/pattern.rb,
lib/card/set/abstract.rb,
lib/card/set/event/all.rb,
lib/card/set/registrar.rb,
lib/card/set/i18n_scope.rb,
lib/card/set/inheritance.rb,
lib/card/set/pattern/all.rb,
lib/card/set/advanced_api.rb,
lib/card/set/card_methods.rb,
lib/card/set/i18n_scanner.rb,
lib/card/set/pattern/base.rb,
lib/card/set/event/options.rb,
lib/card/set/required_field.rb,
lib/card/set/event/callbacks.rb,
lib/card/set/format/haml_paths.rb,
lib/card/set/event/delayed_event.rb,
lib/card/set/pattern/class_methods.rb,
lib/card/set/event/skip_and_trigger.rb,
lib/card/set/format/abstract_format.rb,
lib/card/set/format/abstract_format/wrapper.rb,
lib/card/set/format/abstract_format/view_opts.rb,
lib/card/set/format/abstract_format/haml_views.rb,
lib/card/set/format/abstract_format/view_definition.rb

Overview

A Set is a group of Cards to which Rules may apply. Sets can be as specific as a single card, as general as all cards, or anywhere in between.

Rules can defined onto Sets in two ways:

- **Card rules** are defined in card content. These are generally configured via the

web interface and are thus documented at decko.org/rules.

- **Code rules** can be defined in a 'set module'.

## Set Modules

### File structure

Set modules specify views, events, and other methods for a given set of cards. They are defined in a mod’s set directory. For example, suppose you’ve created a mod 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:

card 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

Note: _the set module’s filename connects it to the set, so both the set_pattern and the set_anchor must correspond to the codename of a card in the database to function correctly. For example, the type/company directory corresponds to the Set card named ‘:company+:type`. Both the :company and :type codenames must exist for this to work properly._

### Writing/Editing set modules

A set module is mostly standard ruby, but the files are quite concise because

 1. the set module's file location is used to autogenerate ruby module definitions
 2. A DSL (Domain-Specific Language) makes common tasks easy.

You can, for example, edit ‘mod/biz/set/type/company.rb`, and add only the following code:

def hello
  "world"
end

No further code is needed for this method to be available to cards with the type Company (as specified by the file’s location). This code will automatically be added to a ruby module named ‘Card::Set::Type::Company`. That module is extended with the Set module, giving it access to the set module DSL.

These important Card::Set subclasses explain more about the DSL

- {Format} introduces format blocks
- {Format::AbstractFormat} covers {Format::AbstractFormat#view view} definitions
- {Event::Api} explains {Event::Api#event event} definitions

### Loading set modules

Whenever you fetch or instantiate a card, the card will automatically include code from all the set modules associated with sets of which it is a member.

For example, say you have a Plaintext card named 'Philipp+address', and you have set

files for the following sets:

 * all cards
 * all Plaintext cards
 * all cards ending in +address

When you run any of these:

    mycard = Card.fetch "Philipp+address"
    mycard = "Philipp+address".card
    mycard = ["Philipp", "address"].card

...then mycard will include the set modules associated with each of those sets in the

above order.

### Abstract set modules

Suppose you have code that you’d like to reuse in more than one set.

Well, set modules are just ruby, so it’s possible to just define a standard ruby module (eg ‘module MySimpleModule…`) in a lib directory and then include that set (`include MySimpleModule`). That will work just fine so long as you only want to add simple ruby code and include it in the base ruby module. But what if you want the reusable code to use the DSL? What if you want to define reusable events, for example? Or if you want to define reusable views on formats?

For this purpose, you can use _abstract set modules_. These are modules that use the set DSL but are not defined directly onto a specific set. Instead, they can be included in a set using the ‘include_set` command.

For example, suppose you create a file at ‘mod/biz/set/abstract/special_views.rb`. And within that file you define a view such as the following:

format :html do
  view :bizzy do
    "I'm so busy"
  end
end

This will create an abstract set that can be included in any other set by invoking ‘include_set Card::Set::Abstract::SpecialViews`. Or just `include_set Abstract::SpecialViews` for short. And then the including set will have access to the “bizzy” view.

Defined Under Namespace

Modules: AdvancedApi, CardMethods, Format, Helpers, I18nScope, Inheritance, Pattern, Registrar, Trait Classes: Abstract, Event, I18nScanner, RequiredField, Type

Constant Summary

Constants included from Helpers

Helpers::SET_PATTERN_TEST_REGEXP

Constants included from Event::Api

Event::Api::OPTIONS

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from I18nScope

mod_name, scope

Methods included from Registrar

extended, finalize_load, process_base_modules, register_set

Methods included from Helpers

#format_module, #format_modules, #method_missing, #modules, #pattern_code, #respond_to_missing?, #set_name_parts, #shortname, #underscored_name

Methods included from AdvancedApi

#assign_type, #attachment, #define_set_from_error, #ensure_set, #setting_opts, #stage_method

Methods included from Format

#before, #format, layout_method_name, #view, view_method_name, view_setting_method_name, wrapper_method_name

Methods included from Inheritance

#include_set, #include_set_formats

Methods included from Trait

#card_accessor, #card_reader, #card_writer, #require_field

Methods included from Event::Api

#event

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Card::Set::Helpers

Class Attribute Details

.basketObject

Returns the value of attribute basket.



134
135
136
# File 'lib/card/set.rb', line 134

def basket
  @basket
end

.modulesObject

Returns the value of attribute modules.



134
135
136
# File 'lib/card/set.rb', line 134

def modules
  @modules
end

.traitsObject

Returns the value of attribute traits.



134
135
136
# File 'lib/card/set.rb', line 134

def traits
  @traits
end

Class Method Details

.resetObject



136
137
138
139
140
141
142
143
144
# File 'lib/card/set.rb', line 136

def reset
  self.modules = {
    base: [],     base_format: {},
    nonbase: {},  nonbase_format: {},
    abstract: {}, abstract_format: {}
  }

  self.basket = {}
end