Sha256: 5d6ac70c15d10f1ed42e0149e0f154685069fe477a0f32014a9001483042d709

Contents?: true

Size: 1.8 KB

Versions: 13

Compression:

Stored size: 1.8 KB

Contents

require 'rubrowser/parser/definition/base'

module Rubrowser
  module Parser
    module Relation
      class Base
        attr_reader :namespace, :caller_namespace, :file, :line

        def initialize(namespace, caller_namespace, file: nil, line: nil)
          @namespace = namespace
          @caller_namespace = caller_namespace
          @file = file
          @line = line
          @is_circular = false
        end

        def namespace
          Definition::Base.new(@namespace, file: file, line: line)
        end

        def caller_namespace
          Definition::Base.new(@caller_namespace, file: file, line: line)
        end

        def circular?
          @is_circular
        end

        def set_circular
          @is_circular = true
        end

        def resolve(definitions)
          possibilities.find do |possibility|
            definitions.any? { |definition| definition == possibility }
          end || possibilities.last
        end

        def ==(other)
          namespace == other.namespace &&
            caller_namespace == other.caller_namespace &&
            circular? == other.circular?
        end

        private

        def possibilities
          if absolute?
            return [
              Definition::Base.new(@namespace.compact, file: file, line: line)
            ]
          end

          possible_parent_namespaces
            .map { |possible_parent| possible_parent + @namespace }
            .push(@namespace)
            .map { |i| Definition::Base.new(i, file: file, line: line) }
        end

        def possible_parent_namespaces
          (@caller_namespace.size - 1)
            .downto(0)
            .map { |i| @caller_namespace[0..i] }
        end

        def absolute?
          @caller_namespace.empty? || @namespace.first.nil?
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rubrowser-2.9.0 lib/rubrowser/parser/relation/base.rb
rubrowser-2.8.0 lib/rubrowser/parser/relation/base.rb
rubrowser-2.7.1 lib/rubrowser/parser/relation/base.rb
rubrowser-2.7.0 lib/rubrowser/parser/relation/base.rb
rubrowser-2.6.0 lib/rubrowser/parser/relation/base.rb
rubrowser-2.5.0 lib/rubrowser/parser/relation/base.rb
rubrowser-2.4.0 lib/rubrowser/parser/relation/base.rb
rubrowser-2.3.0 lib/rubrowser/parser/relation/base.rb
rubrowser-2.2.0 lib/rubrowser/parser/relation/base.rb
rubrowser-2.1.0 lib/rubrowser/parser/relation/base.rb
rubrowser-2.0.0 lib/rubrowser/parser/relation/base.rb
rubrowser-0.3.1 lib/rubrowser/parser/relation/base.rb
rubrowser-0.3.0 lib/rubrowser/parser/relation/base.rb