Sha256: 05f7c2fb42f77fe1667719cf5e5e49795253e703ce58e634d8ea34c55e58c414

Contents?: true

Size: 1.46 KB

Versions: 13

Compression:

Stored size: 1.46 KB

Contents

class Card
  class Format
    module Nesting
      # Formats can have subformats. Lineage can be retraced through "parent" format.
      module Subformat
        # note: while it is possible to have a subformat of a different class,
        # the :format_class value takes precedence over :format.
        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

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

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

        # is format's card the format of the main card on a page?
        def main?
          depth.zero?
        end

        # is format's card the format of the requested card?
        # (can be different from main in certain formats, see override in HtmlFormat)
        def focal?
          depth.zero?
        end

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

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

        private

        def subformat_card subcard
          return subcard if subcard.is_a? Card
          Card.fetch subcard, new: {}
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
card-1.96.8 lib/card/format/nesting/subformat.rb
card-1.96.7 lib/card/format/nesting/subformat.rb
card-1.96.6 lib/card/format/nesting/subformat.rb
card-1.96.5 lib/card/format/nesting/subformat.rb
card-1.96.4 lib/card/format/nesting/subformat.rb
card-1.96.3 lib/card/format/nesting/subformat.rb
card-1.96.2 lib/card/format/nesting/subformat.rb
card-1.96.1 lib/card/format/nesting/subformat.rb
card-1.96.0 lib/card/format/nesting/subformat.rb
card-1.95.3 lib/card/format/nesting/subformat.rb
card-1.95.2 lib/card/format/nesting/subformat.rb
card-1.95.1 lib/card/format/nesting/subformat.rb
card-1.95.0 lib/card/format/nesting/subformat.rb