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



64
65
66
67
68
69
70
# File 'lib/card/format/registration.rb', line 64

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

#extract_class_vars(view, opts) ⇒ Object



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

def extract_class_vars view, opts
  Card::Format::VIEW_VARS.each do |varname|
    next unless (value = opts.delete varname)

    send(varname)[view] = value
  end
end

#extract_view_tags(view, tags) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/card/format/registration.rb', line 51

def extract_view_tags view, tags
  return unless tags

  Array.wrap(tags).each do |tag|
    view_tags[view] ||= {}
    view_tags[view][tag] = true
  end
end

#format_ancestryObject



78
79
80
81
82
# File 'lib/card/format/registration.rb', line 78

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

#interpret_view_opts(view, opts) ⇒ Object



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

def interpret_view_opts view, opts
  extract_class_vars view, opts
  extract_view_tags view, opts.delete(:tags)
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

#tagged(view, tag) ⇒ Object



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

def tagged view, tag
  return unless view && tag && (viewhash = view_tags[view.to_sym])

  viewhash[tag.to_sym]
end

#view_setting_method_name(view, setting_name) ⇒ Object



60
61
62
# File 'lib/card/format/registration.rb', line 60

def view_setting_method_name view, setting_name
  "view_#{view}_#{setting_name}_setting"
end