Module: Card::Set::Format::AbstractFormat

Includes:
Basket, HamlViews
Defined in:
lib/card/set/format.rb

Overview

All Format modules are extended with this module in order to support the basic format API (ok, view definitions. It's just view definitions.) No longer just view definitions. Also basket definitions now.

Instance Method Summary collapse

Methods included from HamlViews

#each_template_path, #haml_block_locals, #haml_template_path, #haml_template_proc, #haml_to_html, #haml_view_block, #template_location, #try_haml_template_path, #with_template_path

Methods included from Basket

#abstract_basket, #add_to_basket, #basket

Instance Method Details

#async_view?(args) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/card/set/format.rb', line 138

def async_view? args
  args.first.is_a?(Hash) && args.first[:async]
end

#define_async_view_method(view, &block) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/card/set/format.rb', line 109

def define_async_view_method view, &block
  view_content = "#{view}_async_content"
  define_standard_view_method view_content, &block
  define_standard_view_method view do
    %(<card-view-placeholder data-url="#{path view: view_content}" />)
  end
end

#define_standard_view_method(view, &block) ⇒ Object



104
105
106
107
# File 'lib/card/set/format.rb', line 104

def define_standard_view_method view, &block
  views[self][view] = block
  define_method "_view_#{view}", &block
end

#extract_view_cache_rules(view, cache_rule) ⇒ Object



123
124
125
126
127
# File 'lib/card/set/format.rb', line 123

def extract_view_cache_rules view, cache_rule
  return unless cache_rule
  methodname = Card::Format.view_cache_setting_method view
  define_method(methodname) { cache_rule }
end

#haml_view?(args) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/card/set/format.rb', line 134

def haml_view? args
  args.first.is_a?(Hash) && args.first[:template] == :haml
end

#interpret_view_opts(view, opts) ⇒ Object



117
118
119
120
121
# File 'lib/card/set/format.rb', line 117

def interpret_view_opts view, opts
  return unless opts.present?
  Card::Format.interpret_view_opts view, opts
  extract_view_cache_rules view, opts.delete(:cache)
end

#lookup_alias_block(view, args) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/card/set/format.rb', line 142

def lookup_alias_block view, args
  opts = args[0].is_a?(Hash) ? args.shift : { view: args.shift }
  opts[:mod] ||= self
  opts[:view] ||= view
  views[opts[:mod]][opts[:view]] || begin
    raise "cannot find #{opts[:view]} view in #{opts[:mod]}; " \
          "failed to alias #{view} in #{self}"
  end
end

#set_moduleObject

remove the format part of the module name



157
158
159
# File 'lib/card/set/format.rb', line 157

def set_module
  Card.const_get name.split("::")[0..-2].join("::")
end

#source_locationObject



152
153
154
# File 'lib/card/set/format.rb', line 152

def source_location
  set_module.source_location
end

#view(view, *args, &block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/card/set/format.rb', line 88

def view view, *args, &block
  #view = view.to_viewname.key.to_sym
  interpret_view_opts view, args[0] if block_given?
  view_method_block = view_block(view, args, &block)
  if async_view? args
    # This case makes only sense for HtmlFormat
    # but I don't see an easy way to override class methods for a specific
    # format. All formats are extended with this general module. So
    # a HtmlFormat.view method would be overridden by AbstractFormat.view
    # We need something like AbstractHtmlFormat for that.
    define_async_view_method view, &view_method_block
  else
    define_standard_view_method view, &view_method_block
  end
end

#view_block(view, args, &block) ⇒ Object



129
130
131
132
# File 'lib/card/set/format.rb', line 129

def view_block view, args, &block
  return haml_view_block(view, &block) if haml_view?(args)
  block_given? ? block : lookup_alias_block(view, args)
end