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.



5
6
7
# File 'lib/modl/parser/modl_method.rb', line 5

def id
  @id
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/modl/parser/modl_method.rb', line 6

def name
  @name
end

#transformObject

Returns the value of attribute transform.



7
8
9
# File 'lib/modl/parser/modl_method.rb', line 7

def transform
  @transform
end

Instance Method Details

#name_or_idObject



9
10
11
# File 'lib/modl/parser/modl_method.rb', line 9

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

#run(str) ⇒ Object

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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/modl/parser/modl_method.rb', line 14

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'
      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'
      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'
      str = str.split.map(&:capitalize) * ' '
      transform = Sutil.tail(transform, 8)
    elsif transform.start_with? 'upcase'
      raise InterpreterError, 'NOT IMPLEMENTED'
    elsif transform.start_with? 'downcase'
      raise InterpreterError, 'NOT IMPLEMENTED'
    elsif transform.start_with? 'sentence'
      raise InterpreterError, 'NOT IMPLEMENTED'
    elsif transform.start_with? 'urlencode'
      raise InterpreterError, 'NOT IMPLEMENTED'
    else
      raise InterpreterError, 'NOT IMPLEMENTED'
    end
  end
  str
end