Sha256: 5ea54b26184b03d10842cb41b871af5090f822c71ef32d6c570aec187ac4549d

Contents?: true

Size: 1.9 KB

Versions: 20

Compression:

Stored size: 1.9 KB

Contents

module Fasterer
  class MethodDefinition
    attr_reader :element # for testing purposes
    attr_reader :method_name
    attr_reader :block_argument_name
    attr_reader :body
    attr_reader :arguments

    alias_method :name, :method_name

    def initialize(element)
      @element = element # Ripper element
      set_method_name
      set_body
      set_arguments
      set_block_argument_name
    end

    def has_block?
      !!@block_argument_name
    end

    def setter?
      name.to_s.end_with?('=')
    end

    private

    def arguments_element
      element[2].drop(1) || []
    end

    def set_method_name
      @method_name = @element[1]
    end

    def set_arguments
      @arguments = arguments_element.map do |argument_element|
        MethodDefinitionArgument.new(argument_element)
      end
    end

    def set_body
      @body = @element[3..-1]
    end

    def set_block_argument_name
      if last_argument_element.to_s.start_with?('&')
        @block_argument_name = last_argument_element.to_s[1..-1].to_sym
      end
    end

    def last_argument_element
      arguments_element.last
    end
  end

  class MethodDefinitionArgument
    attr_reader :element, :name, :type

    def initialize(element)
      @element = element
      set_name
      set_argument_type
    end

    def regular_argument?
      @type == :regular_argument
    end

    def default_argument?
      @type == :default_argument
    end

    def keyword_argument?
      @type == :keyword_argument
    end

    private

    def set_name
      @name = element.is_a?(Symbol) ? element : element[1]
    end

    def set_argument_type
      @type = if element.is_a?(Symbol)
                :regular_argument
              elsif element.is_a?(Sexp) && element.sexp_type == :lasgn
                :default_argument
              elsif element.is_a?(Sexp) && element.sexp_type == :kwarg
                :keyword_argument
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
fasterer-0.11.0 lib/fasterer/method_definition.rb
fasterer-0.10.1 lib/fasterer/method_definition.rb
fasterer-0.10.0 lib/fasterer/method_definition.rb
fasterer-0.9.0 lib/fasterer/method_definition.rb
fasterer-0.8.3 lib/fasterer/method_definition.rb
fasterer-0.8.2 lib/fasterer/method_definition.rb
fasterer-0.8.1 lib/fasterer/method_definition.rb
fasterer-0.8.0 lib/fasterer/method_definition.rb
fasterer-0.7.1 lib/fasterer/method_definition.rb
fasterer-0.7.0 lib/fasterer/method_definition.rb
fasterer-0.6.0 lib/fasterer/method_definition.rb
fasterer-0.5.1 lib/fasterer/method_definition.rb
fasterer-0.5.0 lib/fasterer/method_definition.rb
fasterer-0.4.2 lib/fasterer/method_definition.rb
fasterer-0.4.1 lib/fasterer/method_definition.rb
fasterer-0.4.0 lib/fasterer/method_definition.rb
fasterer-0.3.2 lib/fasterer/method_definition.rb
fasterer-0.3.1 lib/fasterer/method_definition.rb
fasterer-0.3.0 lib/fasterer/method_definition.rb
fasterer-0.2.1 lib/fasterer/method_definition.rb