Module: Card::Format::Registration

Included in:
Card::Format
Defined in:
lib/card/format/registration.rb

Instance Method Summary collapse

Instance Method Details

#class_from_name(formatname) ⇒ Object



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

def class_from_name formatname
  if formatname == "Format"
    Card::Format
  else
    Card::Format.const_get formatname
  end
end

#format_ancestryObject



46
47
48
49
50
# File 'lib/card/format/registration.rb', line 46

def format_ancestry
  ancestry = [self]
  ancestry += superclass.format_ancestry unless self == Card::Format
  ancestry
end

#format_class(opts) ⇒ Object



17
18
19
20
21
22
# File 'lib/card/format/registration.rb', line 17

def format_class opts
  return opts[:format_class] if opts[:format_class]

  format = opts[:format] || :html
  class_from_name format_class_name(format)
end

#format_class_name(format) ⇒ Object



24
25
26
27
28
29
# File 'lib/card/format/registration.rb', line 24

def format_class_name format
  format = format.to_s
  format = "" if format == "base"
  format = aliases[format] if aliases[format]
  "#{format.camelize}Format"
end

#format_sym(format) ⇒ Object



31
32
33
34
35
36
# File 'lib/card/format/registration.rb', line 31

def format_sym format
  return format if format.is_a? Symbol

  match = format.to_s.match(/::(?<format>[^:]+)Format/)
  match ? match[:format].underscore.to_sym : :base
end

#new(card, opts = {}) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/card/format/registration.rb', line 8

def new card, opts={}
  if self != Format
    super
  else
    klass = format_class opts
    self == klass ? super : klass.new(card, opts)
  end
end

#register(format) ⇒ Object



4
5
6
# File 'lib/card/format/registration.rb', line 4

def register format
  registered << format.to_s
end