Module: Card::Format::Nest

Includes:
Fetch, Main, Subformat, View
Included in:
Card::Format
Defined in:
lib/card/format/nest.rb,
lib/card/format/nest/main.rb,
lib/card/format/nest/view.rb,
lib/card/format/nest/fetch.rb,
lib/card/format/nest/subformat.rb

Defined Under Namespace

Modules: Fetch, Main, Subformat, View

Constant Summary collapse

NEST_MODES =
{ new: :edit,
closed_content: :closed,
setup: :edit,
edit: :edit, closed: :closed, layout: :layout,
normal: :normal, template: :template }.freeze

Instance Method Summary collapse

Methods included from View

#hide_view_in_edit_mode?, #modal_nest_view, #view_in_closed_mode, #view_in_edit_mode, #with_nest_mode

Methods included from Subformat

#field_subformat, #subformat

Methods included from Main

#already_mained?, #main!, #main_nest, #main_nest?, #main_nest_options, #wrap_main

Methods included from Fetch

#fetch_nested_card

Instance Method Details

#content_nest(opts = {}) ⇒ Object

nested by another card's content (as opposed to a direct API nest)



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

def content_nest opts={}
  return opts[:comment] if opts.key? :comment # commented nest
  nest_name = opts[:nest_name]
  return main_nest(opts) if main_nest?(nest_name)
  nest nest_name, opts
end

#content_view?(view) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
# File 'lib/card/format/nest.rb', line 69

def content_view? view
  # TODO: this should be specified in view definition
  [
    :core, :content, :titled, :open, :closed, :open_content
  ].member? view.to_sym
end

#default_nest_viewObject



41
42
43
# File 'lib/card/format/nest.rb', line 41

def default_nest_view
  :name
end

#field_nest(field, opts = {}) ⇒ Object

Main difference compared to #nest is that you can use codename symbols to get nested fields

Examples:

home = Card['home'].format
home.nest :self         # => nest for '*self'
home.field_nest :self   # => nest for 'Home+*self'


82
83
84
85
# File 'lib/card/format/nest.rb', line 82

def field_nest field, opts={}
  field = card.cardname.field(field) unless field.is_a? Card
  nest field, opts
end

#implicit_nest_viewObject



36
37
38
39
# File 'lib/card/format/nest.rb', line 36

def implicit_nest_view
  view = voo_items_view || default_nest_view
  Card::View.canonicalize view
end

#interpret_nest_options(nested_card, options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/card/format/nest.rb', line 25

def interpret_nest_options nested_card, options
  options[:nest_name] ||= nested_card.name
  view = options[:view] || implicit_nest_view
  view = Card::View.canonicalize view

  # FIXME: should handle in closed / edit view definitions
  options[:home_view] ||= [:closed, :edit].member?(view) ? :open : view

  [view, options]
end

#nest(cardish, options = {}, &block) ⇒ Object



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

def nest cardish, options={}, &block
  return "" if nest_invisible?
  nested_card = fetch_nested_card cardish, options
  view, options = interpret_nest_options nested_card, options
  nest_render nested_card, view, options, &block
end

#nest_recursion_risk?(view) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/card/format/nest.rb', line 65

def nest_recursion_risk? view
  content_view?(view) && voo.structure
end

#nest_render(nested_card, view, options) ⇒ Object



45
46
47
48
49
50
# File 'lib/card/format/nest.rb', line 45

def nest_render nested_card, view, options
  subformat = nest_subformat nested_card, options, view
  view = subformat.modal_nest_view view
  rendered = count_chars { subformat.optional_render view, options }
  block_given? ? yield(rendered, view) : rendered
end

#nest_subformat(nested_card, opts, view) ⇒ Object



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

def nest_subformat nested_card, opts, view
  return self if reuse_format? opts[:nest_name], view
  sub = subformat nested_card
  sub.main! if opts[:main]
  sub
end

#reuse_format?(nest_name, view) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
# File 'lib/card/format/nest.rb', line 59

def reuse_format? nest_name, view
  nest_name =~ /^_(self)?$/ &&
    card.context_card == card &&
    !nest_recursion_risk?(view)
end