Sha256: 3c57fdb188d356ea42d16a59723b6332d22b65ceea9e0951c2a955007d331fdb

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module Umwelt::Semantic
  class Base
    extend Forwardable
    def_delegators Hanami::Utils::String, :classify, :underscore
    def_delegators :node, :ancestry, :body, :note

    attr_reader :node

    def initialize(node:)
      @node = node
    end

    def s(type, *children)
      Parser::AST::Node.new(type, children)
    end

    def ancestry_path
      ancestry
        .collect(&:body)
        .map { |body| underscore(body) }
        .join '/'
    end

    def code
      Unparser.unparse(ast)
    end

    # default_location defined, for example,
    # in Semantic::Plain::Base
    # or base file from another semantic
    def path(location: nil)
      Pathname.pwd / (location || default_location) / tail_path
    end

    # nil check for case of root node, which has not context
    def context
      node.context&.semantic(:Plain)
    end

    # classified symbol
    def csymbol
      classify(body).to_sym
    end

    def label
      self.class.name.split('::').last.to_sym
    end

    def tail_path
      Pathname.new(ancestry_path).sub_ext('.rb')
    end
  end
end

require_relative './semantic/plain'

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
umwelt-0.2 lib/umwelt/semantic.rb
umwelt-0.1.1 lib/umwelt/semantic.rb
umwelt-0.1.0 lib/umwelt/semantic.rb