Sha256: 5f99e39f807429fc048720d0d4f3fe1ffc06690b76c1c35e208c2c9aec7295a9

Contents?: true

Size: 1.65 KB

Versions: 17

Compression:

Stored size: 1.65 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 main
          if main?
            self
          elsif !root?
            parent.main
          end
        end

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

        def root?
          depth.zero?
        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?
          @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

17 entries across 17 versions & 1 rubygems

Version Path
card-1.101.4 lib/card/format/nesting/subformat.rb
card-1.101.3 lib/card/format/nesting/subformat.rb
card-1.101.2 lib/card/format/nesting/subformat.rb
card-1.101.1 lib/card/format/nesting/subformat.rb
card-1.101.0 lib/card/format/nesting/subformat.rb
card-1.100.0 lib/card/format/nesting/subformat.rb
card-1.99.6 lib/card/format/nesting/subformat.rb
card-1.99.5 lib/card/format/nesting/subformat.rb
card-1.99.4 lib/card/format/nesting/subformat.rb
card-1.99.3 lib/card/format/nesting/subformat.rb
card-1.99.2 lib/card/format/nesting/subformat.rb
card-1.99.1 lib/card/format/nesting/subformat.rb
card-1.99.0 lib/card/format/nesting/subformat.rb
card-1.98.3 lib/card/format/nesting/subformat.rb
card-1.98.2 lib/card/format/nesting/subformat.rb
card-1.98.1 lib/card/format/nesting/subformat.rb
card-1.98.0 lib/card/format/nesting/subformat.rb