Module: Card::Format::Render

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

Overview

View rendering methods.

Constant Summary collapse

DEPRECATED_VIEWS =
{
  view: :open, card: :open, line: :closed, bare: :core, naked: :core
}.freeze

Instance Method Summary collapse

Instance Method Details

#canonicalize_view(view) ⇒ Object



47
48
49
50
51
# File 'lib/card/format/render.rb', line 47

def canonicalize_view view
  return if view.blank?
  view_key = view.to_viewname.key.to_sym
  DEPRECATED_VIEWS[view_key] || view_key
end

#configured_visibility(view, args, code_config) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/card/format/render.rb', line 71

def configured_visibility view, args, code_config
  card_config = nest_arg_visibility view, args
  case (card_config || code_config)
  when :show then true
  when :hide then false
  else
    default_viz = args.delete :default_visibility
    default_viz ? default_viz == :show : true
  end
end

#current_view(view) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/card/format/render.rb', line 112

def current_view view
  old_view = @current_view
  @current_view = view
  yield
ensure
  @current_view = old_view
end

#default_item_viewObject



53
54
55
# File 'lib/card/format/render.rb', line 53

def default_item_view
  :name
end

#default_render_args(view, a = nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/card/format/render.rb', line 98

def default_render_args view, a=nil
  args =
    case a
    when nil   then {}
    when Hash  then a.clone
    when Array then a[0].merge a[1]
    else raise Card::Error, "bad render args: #{a}"
    end

  default_method = "default_#{view}_args"
  send default_method, args if respond_to?(default_method)
  args
end

#hidden_view?(view, args) ⇒ Boolean

Returns:

  • (Boolean)


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

def hidden_view? view, args
  args.delete(:optional) && !show_view?(view, args)
end

#nest_arg_visibility(view, args) ⇒ Object



82
83
84
85
86
87
# File 'lib/card/format/render.rb', line 82

def nest_arg_visibility view, args
  [:show, :hide].each do |setting|
    return setting if parse_view_visibility(args[setting]).member?(view)
  end
  false
end

#optional_render_args(args, opts) ⇒ Object



35
36
37
38
# File 'lib/card/format/render.rb', line 35

def optional_render_args args, opts
  args[:optional] = true
  args[:default_visibility] = opts.shift
end

#parse_view_visibility(val) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/card/format/render.rb', line 89

def parse_view_visibility val
  case val
  when NilClass then []
  when Array    then val
  when String   then val.split(/[\s,]+/)
  else raise Card::Error, "bad show/hide argument: #{val}"
  end.map { |view| canonicalize_view view }
end

#render(view, args = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/card/format/render.rb', line 10

def render view, args={}
  view = canonicalize_view view
  return if hidden_view? view, args
  view = ok_view view, args
  current_view(view) do
    args = default_render_args view, args
    with_nest_mode view do
      Card::Cache::ViewCache.fetch(self, view, args) do
        method = view_method view, args
        method.arity.zero? ? method.call : method.call(args)
      end
    end
  end
rescue => e
  rescue_view e, view
end

#render_api(match, opts) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/card/format/render.rb', line 27

def render_api match, opts
  view = match[3] ? match[4] : opts.shift
  args = opts[0] ? opts.shift.clone : {}
  optional_render_args(args, opts) if match[2]
  args[:skip_permissions] = true if match[1]
  render view, args
end

#show_view?(view, args) ⇒ Boolean

Returns:

  • (Boolean)


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

def show_view? view, args
  code_config = args["optional_#{view}".to_sym]
  case code_config
    # permanent visibility specified in code
  when :always then true
  when :never  then false
  else configured_visibility view, args, code_config
  end
end

#view_method(view, args) ⇒ Object



40
41
42
43
44
45
# File 'lib/card/format/render.rb', line 40

def view_method view, args
  method "_view_#{view}"
rescue
  args[:unsupported_view] = view
  method "_view_unsupported_view"
end