Module: Card::Set::Format

Included in:
Card::Set
Defined in:
lib/card/set/format.rb,
lib/card/set/format/haml_views.rb

Overview

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

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)

Defined Under Namespace

Modules: AbstractFormat, HamlViews

Constant Summary collapse

TEMPLATE_DIR =
%w[template set].freeze

Instance Method Summary collapse

Instance Method Details

#all_set_format_mod!(format_class, mod) ⇒ Object

make mod ready to include in base (non-set-specific) format classes



72
73
74
75
# File 'lib/card/set/format.rb', line 72

def all_set_format_mod! format_class, mod
  modules[:base_format][format_class] ||= []
  modules[:base_format][format_class] << mod
end

#define_on_format(format_name = :base, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/card/set/format.rb', line 44

def define_on_format format_name=:base, &block
  # format class name, eg. HtmlFormat
  klass = Card::Format.format_class_name format_name

  # called on current set module, eg Card::Set::Type::Pointer
  mod = const_get_or_set klass do
    # yielding set format module, eg Card::Set::Type::Pointer::HtmlFormat
    m = Module.new
    register_set_format Card::Format.class_from_name(klass), m
    m.extend Card::Set::AbstractFormat
    m
  end
  mod.class_eval(&block)
end

#format(*format_names, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/card/set/format.rb', line 25

def format *format_names, &block
  format_names.compact!
  if format_names.empty?
    format_names = [:base]
  elsif format_names.first == :all
    format_names =
      Card::Format.registered.reject { |f| Card::Format.aliases[f] }
  end
  format_names.each do |f|
    define_on_format f, &block
  end
end

#register_set_format(format_class, mod) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/card/set/format.rb', line 59

def register_set_format format_class, mod
  if all_set?
    all_set_format_mod! format_class, mod
  else
    format_type = abstract_set? ? :abstract_format : :nonbase_format
    # ready to include dynamically in set members' format singletons
    format_hash = modules[format_type][format_class] ||= {}
    format_hash[shortname] ||= []
    format_hash[shortname] << mod
  end
end

#view(*args, &block) ⇒ Object



38
39
40
41
42
# File 'lib/card/set/format.rb', line 38

def view *args, &block
  format do
    view(*args, &block)
  end
end