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



61
62
63
64
65
66
67
# File 'lib/card/format/registration.rb', line 61

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

#extract_class_vars(view, opts) ⇒ Object



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

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



49
50
51
52
53
54
55
# File 'lib/card/format/registration.rb', line 49

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



74
75
76
77
78
# File 'lib/card/format/registration.rb', line 74

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

#format_class(opts) ⇒ Object



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

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
# 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



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

def interpret_view_opts view, opts
  extract_class_vars view, opts
  extract_view_tags view, opts.delete(:tags)
end

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



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

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



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

def register format
  registered << format.to_s
end

#tagged(view, tag) ⇒ Object



69
70
71
72
# File 'lib/card/format/registration.rb', line 69

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

#view_cache_setting_method(view) ⇒ Object



57
58
59
# File 'lib/card/format/registration.rb', line 57

def view_cache_setting_method view
  "view_#{view}_cache_setting"
end