Module: Card::Format::Nesting::Subformat

Included in:
Card::Format::Nesting
Defined in:
lib/card/format/nesting/subformat.rb

Overview

Formats can have subformats. Lineage can be retraced through "parent" format.

Instance Method Summary collapse

Instance Method Details

#depthObject



18
19
20
# File 'lib/card/format/nesting/subformat.rb', line 18

def depth
  @depth ||= parent ? (parent.depth + 1) : 0
end

#field_subformat(field) ⇒ Object



33
34
35
36
# File 'lib/card/format/nesting/subformat.rb', line 33

def field_subformat field
  field = card.name.field(field) unless field.is_a?(Card)
  subformat field
end

#focal?Boolean

is format's card the format of the requested card? (can be different from main in certain formats, see override in HtmlFormat)

Returns:

  • (Boolean)


29
30
31
# File 'lib/card/format/nesting/subformat.rb', line 29

def focal?
  depth.zero?
end

#inherit(variable) ⇒ Object



38
39
40
41
# File 'lib/card/format/nesting/subformat.rb', line 38

def inherit variable
  variable = "@#{variable}" unless variable.to_s.start_with? "@"
  instance_variable_get(variable) || parent&.inherit(variable)
end

#main?Boolean

is format's card the format of the main card on a page?

Returns:

  • (Boolean)


23
24
25
# File 'lib/card/format/nesting/subformat.rb', line 23

def main?
  depth.zero?
end

#rootObject



14
15
16
# File 'lib/card/format/nesting/subformat.rb', line 14

def root
  @root ||= parent ? parent.root : self
end

#subformat(subcard, opts = {}) ⇒ Object

note: while it is possible to have a subformat of a different class, the :format_class value takes precedence over :format.



8
9
10
11
12
# File 'lib/card/format/nesting/subformat.rb', line 8

def subformat subcard, opts={}
  subcard = subformat_card subcard
  opts = opts.merge(parent: self).reverse_merge(format_class: self.class)
  self.class.new subcard, opts
end