Module: Card::Set::Inheritance

Included in:
Card::Set
Defined in:
lib/card/set/inheritance.rb

Overview

API to inherit other sets and their formats

Instance Method Summary collapse

Instance Method Details

#include_set(set, opts = {}) ⇒ Object

include a set module and all its format modules

Examples:

include_set Type::Basic, except: :css

pass an option

include_set Type::Name, default_name: "Luke"
default_name           # => "Luke"

def introduce_yourself
  puts my_name_is      # => "Luke"
end

# in Type::Name
def self.included host_class
  host_class.class_eval do
    define_method :my_name_is do |name=nil|
      name || host_class.default_name
    end
  end
end

Parameters:

  • set (Module)
  • opts (Hash) (defaults to: {})

    choose the formats you want to include. You can also pass arbitrary options to the included set. The option is saved in the including set. To use the option you need a ‘included` method in the included set to fetch the option.

Options Hash (opts):

  • :only (Symbol, Array<Symbol>)

    include only these formats

  • :except (Symbol, Array<Symbol>)

    don’t include these formats



34
35
36
37
38
39
40
41
42
43
# File 'lib/card/set/inheritance.rb', line 34

def include_set set, opts={}
  opts.each do |key, value|
    cattr_accessor key
    send "#{key}=", value
  end

  set_type = set.abstract_set? ? :abstract : :nonbase
  add_set_modules Card::Set.modules[set_type][set.shortname]
  include_set_formats set, opts
end

#include_set_formats(set, opts = {}) ⇒ Object

include format modules of a set

Examples:

include_set_formats Type::Basic, except: :css

Parameters:

  • set (Module)
  • opts (Hash) (defaults to: {})

    choose the formats you want to include

Options Hash (opts):

  • :only (Symbol, Array<Symbol>)

    include only these formats

  • :except (Symbol, Array<Symbol>)

    don’t include these formats



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/card/set/inheritance.rb', line 52

def include_set_formats set, opts={}
  each_format set do |format, format_mods|
    format_sym = Card::Format.format_sym format
    next unless applicable_format?(format_sym, opts[:except], opts[:only])

    format_mods.each do |format_mod|
      define_on_format format_sym do
        include format_mod
      end
    end
  end
end