module Card::Format::Nesting::Subformat

Formats can have subformats. Lineage can be retraced through “parent” format.

Public Instance Methods

depth() click to toggle source
# File lib/card/format/nesting/subformat.rb, line 26
def depth
  @depth ||= parent ? (parent.depth + 1) : 0
end
field_subformat(field) click to toggle source
# File lib/card/format/nesting/subformat.rb, line 45
def field_subformat field
  field = card.name.field(field) unless field.is_a?(Card)
  subformat field
end
focal?() click to toggle source

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

# File lib/card/format/nesting/subformat.rb, line 41
def focal?
  @focal ||= depth.zero?
end
inherit(variable) click to toggle source
# File lib/card/format/nesting/subformat.rb, line 50
def inherit variable
  variable = "@#{variable}" unless variable.to_s.start_with? "@"
  instance_variable_get(variable) || parent&.inherit(variable)
end
main() click to toggle source
# File lib/card/format/nesting/subformat.rb, line 18
def main
  if main?
    self
  elsif !root?
    parent.main
  end
end
main?() click to toggle source

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

# File lib/card/format/nesting/subformat.rb, line 35
def main?
  depth.zero?
end
root() click to toggle source
# File lib/card/format/nesting/subformat.rb, line 14
def root
  @root ||= parent ? parent.root : self
end
root?() click to toggle source
# File lib/card/format/nesting/subformat.rb, line 30
def root?
  depth.zero?
end
subformat(subcard, opts={}) click to toggle source

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

# 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

Private Instance Methods

subformat_card(subcard) click to toggle source
# File lib/card/format/nesting/subformat.rb, line 57
def subformat_card subcard
  return subcard if subcard.is_a? Card

  Card.fetch subcard, new: {}
end