class Xumlidot::Parsers::Args

Parser for the arguments to a method

e.g. formats def method(a, b = nil)

to a string 'a, b = nil'

Public Class Methods

new(exp) click to toggle source
Calls superclass method
# File lib/xumlidot/parsers/args.rb, line 15
def initialize(exp)
  super()

  @arguments = ::Xumlidot::Types::Arguments.new

  process(exp)
rescue => e
  STDERR.puts " ** bug: unable to process args #{exp} "
end

Public Instance Methods

definition() click to toggle source
# File lib/xumlidot/parsers/args.rb, line 29
def definition
  @arguments
end
process_args(exp) click to toggle source
# File lib/xumlidot/parsers/args.rb, line 141
def process_args(exp)
  exp.shift # remove :args
  exp.each do |arg|
    @argument = ::Xumlidot::Types::Argument.new
    if arg.is_a? Sexp
      @argument.assign = '='
      process(arg)
    else
      @argument.name = arg.to_s
    end

    @arguments << @argument
  end
rescue => e
  STDERR.puts " ** bug: unable to process args #{exp}; failure to parse default value? "
end
process_array(exp) click to toggle source
# File lib/xumlidot/parsers/args.rb, line 90
def process_array(exp)
  @argument.default = []

  exp.shift # remove :array
  exp.each { |element| process(element) }
  s()
end
process_colon2(exp) click to toggle source

Colon2 means that we have a constant assignment such as (a = Foo::Bar)

# File lib/xumlidot/parsers/args.rb, line 59
def process_colon2(exp)
  name = exp.flatten
  name.delete :const
  name.delete :colon2

  leader = ''
  if name.first == :colon3
    leader = '::'
    name.delete :colon3
  end

  # I'm not sure how best to proceed here.
  #
  # I can use const_set to start creating the constants heirachy
  # but this is complex since it needs to be inserted into the right
  # place and for that I need the namespace...which suggests this ISNT
  # the place to do that. I possibly need a fake class ...
  @argument.default = leader + name.map do |v|
    v.to_s
  end.to_a.join('::')

  s()
end
process_colon3(exp) click to toggle source

Colon2 means that we have a constant assignment such as (a = ::Foo::Bar) again see the note in colon2 about how to proceed

# File lib/xumlidot/parsers/args.rb, line 85
def process_colon3(exp)
  process_colon2(exp)
  @argument.default = "::#{argument.default}"
end
process_const(exp) click to toggle source

const means that we have a constant assignment such as (a = Foo)

# File lib/xumlidot/parsers/args.rb, line 53
def process_const(exp)
  @argument.default = exp.value.to_s
  s()
end
process_hash(exp) click to toggle source
# File lib/xumlidot/parsers/args.rb, line 47
def process_hash(exp)
  @argument.default = {}
  s()
end
process_kwarg(exp) click to toggle source
# File lib/xumlidot/parsers/args.rb, line 131
def process_kwarg(exp)
  exp.shift # remove :kwarg
  @argument.name = "#{exp[0]}:"
  process(exp)
  s()
rescue => e
  STDERR.puts " ** bug: unable to process kwarg #{exp}; failure to parse default value? "
  s()
end
process_lasgn(exp) click to toggle source
# File lib/xumlidot/parsers/args.rb, line 98
def process_lasgn(exp)
  exp.shift # remove :lasgn

  @argument.name = exp.shift.to_s

  value = exp.shift
  process(value)
  s()
end
process_lit(exp) click to toggle source
# File lib/xumlidot/parsers/args.rb, line 108
def process_lit(exp)
  exp.shift # remove :lit

  case @argument.default
  when Array
    @argument.default << exp.value
  when nil
    @argument.default = exp.value
  when Symbol
    @argument.default = exp.value.to_s
  when String
    @argument.default = exp.value.to_s
  when Sexp
    # binding.pry
  when Hash
    # binding.pry
  else
    # binding.pry
  end
  exp.shift
  s()
end
process_nil(exp) click to toggle source

Note - special case since a value of nil for default means we shouldn't display it and so we use the :nil symbol to represent an *actual assignment of nil* to a variable.

# File lib/xumlidot/parsers/args.rb, line 37
def process_nil(exp)
  @argument.default = :nil
  s()
end
process_str(exp) click to toggle source
# File lib/xumlidot/parsers/args.rb, line 42
def process_str(exp)
  @argument.default = exp.value
  s()
end
to_s() click to toggle source
# File lib/xumlidot/parsers/args.rb, line 25
def to_s
  @arguments.to_s
end