Class: MODL::Parser::MODLMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/modl/parser/modl_method.rb

Overview

Represents a *method defined by a MODL document.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject

Returns the value of attribute id.



29
30
31
# File 'lib/modl/parser/modl_method.rb', line 29

def id
  @id
end

#nameObject

Returns the value of attribute name.



30
31
32
# File 'lib/modl/parser/modl_method.rb', line 30

def name
  @name
end

#transformObject

Returns the value of attribute transform.



31
32
33
# File 'lib/modl/parser/modl_method.rb', line 31

def transform
  @transform
end

Instance Method Details

#name_or_idObject



33
34
35
# File 'lib/modl/parser/modl_method.rb', line 33

def name_or_id
  @name.nil? ? @id : @name
end

#run(str) ⇒ Object

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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