Module: Card::Set::Format

Defined in:
lib/card/set.rb

Overview

A ‘Set’ is a group of Cards to which ‘Rules’ may be applied.

Sets can be as specific as a single card, as general as all cards, or
anywhere in between.

Rules take two main forms: card rules and code rules.

'Card rules' are defined in card content. These are generally configured
via the web interface and are thus documented at http://wagn.org/rules.

'Code rules' can be defined in a 'set file' within any 'Mod' (short for
both 'module' and 'modification'). In accordance with Wagn's 'MoVE'
architecture, there are two main kinds of code rules you can create in
set file: Views, and Events.
Events are associated with the Card class, and Views are associated with
a Format class.
You can also use set files to add or override Card and/or Format methods
directly.  The majority of Card code is contained in these files.

    (FIXME - define mod, add generator)

Whenever you fetch or instantiate a card, it will automatically include
all the set modules defined in set files associated with sets of which it
is a member.  This entails both simple model methods and 'events', which
are special methods explored in greater detail below.

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 this:

    mycard = Card.fetch 'Philipp+address'

...then mycard will include the set modules associated with each of those
ets in the above order.  (The order is determined by the set pattern;
ee lib/card/set_pattern.rb for more information about set_ptterns and
od/core/set/all/fetch.rb for more about fetching.)

imilarly, whenever a Format object is instantiated for a card, it
ncludes all views associated with BOTH (a) sets of which the card is a
ember and (b) the current format or its ancestors.  More on defining
iews below.

In order to have a set file associated with "all cards ending in
+address", you could create a file in
mywagn/mod/mymod/set/right/address.rb.
The recommended mechanism for doing so is running `wagn generate set
modname set_pattern set_anchor`. In the current example, this
would translate to `wagn generate set mymod right address`.
Note that both the set_pattern and the set_anchor must correspond to the
codename of a card in the database to function correctly but you can add
arbitrary subdirectories to organize your code rules. The rule above
for example could be saved in
mywagn/mod/mymod/set/right/address/america/north/canada.rb.

When a Card application loads, it uses these files to autogenerate a
tmp_file that uses this set file to create a Card::Set::Right::Address
module which itself is extended with Card::Set. A set file is 'just ruby'
but is generally quite concise because Card uses its file location to
autogenerate ruby module names and then uses Card::Set module to provide
additional API.

View definitions

When you declare:
  view :view_name do |args|
    #...your code here
  end

Methods are defined on the format

The external api with checks:
  render(:viewname, args)

Constant Summary collapse

@@views =
{}

Instance Method Summary collapse

Instance Method Details

#alias_block(view, args) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/card/set.rb', line 106

def alias_block view, args
  opts = args[0].is_a?(Hash) ? args.shift : { view: args.shift }
  opts[:mod] ||= self
  opts[:view] ||= view
  views[opts[:mod]][opts[:view]] || raise
rescue
  raise "cannot find #{opts[:view]} view in #{opts[:mod]}; " \
        "failed to alias #{view} in #{self}"
end

#view(view, *args, &block) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/card/set.rb', line 93

def view view, *args, &block
  view = view.to_viewname.key.to_sym
  views[self] ||= {}
  view_block = views[self][view] =
                 if block_given?
                   Card::Format.extract_class_vars view, args[0]
                   block
                 else
                   alias_block view, args
                 end
  define_method "_view_#{view}", view_block
end