Sha256: 03a39e52028754f8b25c0b18b2f4b30e8cce0181c506df5fbb6b6fcff883d2ad

Contents?: true

Size: 1.63 KB

Versions: 5

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module Vedeu

  module Views

    # A collection of {Vedeu::Cells::Char} instances.
    #
    # @api private
    #
    class Chars < Vedeu::Repositories::Collection

      # @param collection [void]
      # @param parent [Vedeu::Views::Stream]
      # @param name [NilClass|Symbol|String]
      # @macro raise_invalid_syntax
      # @return [Vedeu::Views::Chars]
      def self.coerce(collection = [], parent = nil, name = nil)
        if collection.is_a?(Vedeu::Views::Chars)
          collection

        elsif collection.is_a?(Array)
          return new(collection, parent, name) if collection.empty?

          coerced_collection = []
          collection.each do |element|
            coerced_collection << element if element.is_a?(Vedeu::Cells::Char)
          end

          new(coerced_collection, parent, name)

        elsif collection.is_a?(Vedeu::Views::Stream)
          Vedeu::Views::Chars.coerce(collection.value,
                                     collection.parent,
                                     collection.name)

        elsif collection.is_a?(String)
          return new([], parent, name) if collection.empty?

          if parent && parent.attributes
            new_collection = Vedeu::DSL::Text.new(collection,
                                                  parent.attributes).chars

            new(new_collection, parent, name)
          end

        else
          fail Vedeu::Error::InvalidSyntax,
               'Cannot coerce for Vedeu::View::Chars, as collection is ' \
               'unrecognised.'

        end
      end

      alias chars value

    end # Chars

  end # Views

end # Vedeu

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
vedeu-0.8.12 lib/vedeu/views/chars.rb
vedeu-0.8.10 lib/vedeu/views/chars.rb
vedeu-0.8.9 lib/vedeu/views/chars.rb
vedeu-0.8.8 lib/vedeu/views/chars.rb
vedeu-0.8.7 lib/vedeu/views/chars.rb