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

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

Overview

All Format modules are extended with this module in order to support the basic format API, including view, layout, and basket definitions

Constant Summary collapse

VIEW_SETTINGS =
%i[cache modal bridge wrap].freeze

Instance Attribute Summary

Attributes included from Wrapper

#interior

Instance Method Summary collapse

Methods included from Wrapper

#layout, #wrapper

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)


88
89
90
# File 'lib/card/set/format/abstract_format.rb', line 88

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

#before(view, &block) ⇒ Object



18
19
20
# File 'lib/card/set/format/abstract_format.rb', line 18

def before view, &block
  define_method "_before_#{view}", &block
end

#define_async_view_method(view, &block) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/card/set/format/abstract_format.rb', line 50

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



45
46
47
48
# File 'lib/card/set/format/abstract_format.rb', line 45

def define_standard_view_method view, &block
  views[self][view] = block
  define_method Card::Set::Format.view_method_name(view), &block
end

#define_view_setting_method(view, setting_name, setting_value) ⇒ Object



67
68
69
70
71
72
# File 'lib/card/set/format/abstract_format.rb', line 67

def define_view_setting_method view, setting_name, setting_value
  return unless setting_value

  method_name = Card::Format.view_setting_method_name view, setting_name
  define_method(method_name) { setting_value }
end

#haml_view?(args) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/card/set/format/abstract_format.rb', line 80

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

#interpret_view_opts(view, opts) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/card/set/format/abstract_format.rb', line 58

def interpret_view_opts view, opts
  return unless opts.present?

  Card::Format.interpret_view_opts view, opts
  VIEW_SETTINGS.each do |setting_name|
    define_view_setting_method view, setting_name, opts.delete(setting_name)
  end
end

#lookup_alias_block(view, args) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/card/set/format/abstract_format.rb', line 92

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



107
108
109
# File 'lib/card/set/format/abstract_format.rb', line 107

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

#source_locationObject



102
103
104
# File 'lib/card/set/format/abstract_format.rb', line 102

def source_location
  set_module.source_location
end

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/card/set/format/abstract_format.rb', line 22

def view view, *args, &block
  # binding.pry
  # 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



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

def view_block view, args, &block
  return haml_view_block(view, wrap_with_slot?(args), &block) if haml_view?(args)

  block_given? ? block : lookup_alias_block(view, args)
end

#view_for_override(viewname) ⇒ Object



39
40
41
42
43
# File 'lib/card/set/format/abstract_format.rb', line 39

def view_for_override viewname
  view viewname do
    "override '#{viewname}' view"
  end
end

#wrap_with_slot?(args) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/card/set/format/abstract_format.rb', line 84

def wrap_with_slot? args
  args.first.is_a?(Hash) && args.first[:slot]
end