Module: Card::Set::Helpers

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

Overview

advanced set module API

Instance Method Summary collapse

Instance Method Details

#abstract_set?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/card/set/helpers.rb', line 91

def abstract_set?
  name =~ /^Card::Set::Abstract::/
end

#all_set?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/card/set/helpers.rb', line 95

def all_set?
  name =~ /^Card::Set::All::/
end

#attachment(name, args) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/card/set/helpers.rb', line 57

def attachment name, args
  include Abstract::Attachment
  add_attributes name, "remote_#{name}_url".to_sym, :load_from_mod,
                 :action_id_of_cached_upload, :empty_ok
  uploader_class = args[:uploader] || FileUploader
  mount_uploader name, uploader_class
end

#ensure_set(&block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/card/set/helpers.rb', line 40

def ensure_set &block
  set_module = yield
rescue NameError => e
  if e.message =~ /uninitialized constant (?:Card::Set::)?(.+)$/
    constant_pieces = Regexp.last_match(1).split('::')
    constant_pieces.inject(Card::Set) do |set_mod, module_name|
      set_mod.const_get_or_set module_name do
        Module.new
      end
    end
  end
  # try again - there might be another submodule that doesn't exist
  ensure_set(&block)
else
  set_module.extend Card::Set
end

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

include a set module and all its format modules include_set 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



12
13
14
15
16
17
18
# File 'lib/card/set/helpers.rb', line 12

def include_set set, opts={}
  set_type = set.abstract_set? ? :abstract : :nonbase
  Card::Set.modules[set_type][set.shortname].each do |set_mod|
    include set_mod
  end
  include_set_formats set, opts
end

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

include a format modules of a set include_set 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



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/card/set/helpers.rb', line 27

def include_set_formats set, opts={}
  each_format set do |format, format_mods|
    match = format.to_s.match(/::(?<format>[^:]+)Format/)
    format_sym = match ? match[:format] : :base
    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

#shortnameObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/card/set/helpers.rb', line 78

def shortname
  parts = name.split '::'
  first = 2 # shortname eliminates Card::Set
  pattern_name = parts[first].underscore
  last = if pattern_name == 'abstract'
           first + 1
         else
           set_class = Card::SetPattern.find pattern_name
           first + set_class.anchor_parts_count
         end
  parts[first..last].join '::'
end

#stage_method(method, opts = {}, &block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/card/set/helpers.rb', line 65

def stage_method method, opts={}, &block
  class_eval do
    define_method "_#{method}", &block
    define_method method do |*args|
      if (error = wrong_stage(opts) || wrong_action(opts[:on]))
        raise Card::Error, error
      else
        send "_#{method}", *args
      end
    end
  end
end