class MODL::Parser::MODLMethod

Represents a *method defined by a MODL document.

Attributes

id[RW]
name[RW]
transform[RW]

Public Instance Methods

name_or_id() click to toggle source
# File lib/modl/parser/modl_method.rb, line 33
def name_or_id
  @name.nil? ? @id : @name
end
run(str) click to toggle source

There is a user-defined method transform to run on the str

# File lib/modl/parser/modl_method.rb, line 38
def run(str)
  # Consume the elements of the transform spec until there are none left.
  transform = @transform
  while transform && transform.length > 0
    if transform.start_with?('replace<') || transform.start_with?('r<')
      close_bracket = transform.index('>')
      m = Sutil.head(transform, close_bracket + 1).sub!('replace', 'r')
      str = StandardMethods.run_method(m, str)
      # Consume the subst clause
      close_bracket = transform.index('>')
      transform = Sutil.tail(transform, close_bracket + 2)
    elsif transform.start_with?('trim') || transform.start_with?('t<')
      close_bracket = transform.index('>')
      m = Sutil.head(transform, close_bracket + 1).sub!('trim', 't')
      str = StandardMethods.run_method(m, str)
      # Consume the trunc clause
      close_bracket = transform.index('>')
      transform = Sutil.tail(transform, close_bracket + 2)
    elsif transform.start_with?('initcap') || transform.start_with?('i')
      str = StandardMethods.run_method('i', str)
      transform = Sutil.after(transform, '.')
    elsif transform.start_with?('upcase') || transform.start_with?('u')
      str = StandardMethods.run_method('u', str)
      transform = Sutil.after(transform, '.')
    elsif transform.start_with?('downcase') || transform.start_with?('d')
      str = StandardMethods.run_method('d', str)
      transform = Sutil.after(transform, '.')
    elsif transform.start_with?('sentence') || transform.start_with?('s')
      str = StandardMethods.run_method('s', str)
      transform = Sutil.after(transform, '.')
    elsif transform.start_with?('urlencode') || transform.start_with?('e')
      str = StandardMethods.run_method('e', str)
      transform = Sutil.after(transform, '.')
    else
      raise InterpreterError, 'NOT IMPLEMENTED'
    end
  end
  str
end