Sha256: 2241c6fcaed0ef07618660ab21681a8a5dd8c936c1e4b9737807e7fda546b1ca

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

require 'songbooks/components/chord'
require 'songbooks/components/literal'

module Songbooks
  module Components
    class Section

      #----------------------------------------------------------------
      #                           CONSTANTS
      #----------------------------------------------------------------

      REGEXP = /^##\s*(.*)$/

      #----------------------------------------------------------------
      #                            Parsing
      #----------------------------------------------------------------

      #
      # Tries to parse the given text line as a section name identifier
      #
      # @return [Hash, NilClass] the parsed information or +nil+ if the given row
      #   is not a valid section row
      #
      def self.parse_information(content)
        if content =~ REGEXP
          return {name: $1}
        end
        nil
      end

      def initialize(name, initial = false)
        @name          = name
        @content_lines = []
        @initial       = initial
      end

      #
      # Parses the given line (chords and literals) and adds it to this section
      #
      def parse_line!(_line)
        line = _line.dup.rstrip
        line_components = []

        until line.length.zero?
          component = Songbooks::Components::Chord.munch_chord(line)
          component ||= Songbooks::Components::Literal.munch_literal(line)
          line_components << component
        end

        add_line(line_components)
      end

      def add_line(line)
        @content_lines << line
      end

      def name
        @name == 'Initial' ? '' : @name
      end

      def lines
        @content_lines
      end

      def transpose!(amount)
        lines.flatten.each do |component|
          next unless component.is_a?(Songbooks::Components::Chord)
          component.transpose!(amount)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
songbooks-0.1.0 lib/songbooks/components/section.rb