Class: TwitterCldr::Transforms::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_cldr/transforms/rule.rb

Overview

Base class for all transform rules

Constant Summary collapse

STRING_TYPES =
[
  :escaped_char, :unicode_char, :escaped_backslash,
  :quoted_string, :doubled_quote
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.regexp_token_string(tokens) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/twitter_cldr/transforms/rule.rb', line 53

def regexp_token_string(tokens)
  tokens.inject('') do |ret, token|
    val = token_value(token)

    ret + case token.type
      when *STRING_TYPES
        Regexp.escape(val)
      else
        val
    end
  end
end

.remove_comment(rule_text) ⇒ Object


66
67
68
69
70
71
72
73
# File 'lib/twitter_cldr/transforms/rule.rb', line 66

def remove_comment(rule_text)
  # comment must come after semicolon
  if rule_idx = rule_text.index(/;[\s]*#/)
    rule_text[0..rule_idx]
  else
    rule_text
  end
end

.replace_symbols(tokens, symbol_table) ⇒ Object


19
20
21
22
23
24
25
26
27
# File 'lib/twitter_cldr/transforms/rule.rb', line 19

def replace_symbols(tokens, symbol_table)
  tokens.inject([]) do |ret, token|
    ret + if token.type == :variable
      symbol_table[token.value].value_tokens
    else
      Array(token)
    end
  end
end

.token_string(tokens) ⇒ Object


47
48
49
50
51
# File 'lib/twitter_cldr/transforms/rule.rb', line 47

def token_string(tokens)
  tokens.inject('') do |ret, token|
    ret + token_value(token)
  end
end

.token_value(token) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/twitter_cldr/transforms/rule.rb', line 29

def token_value(token)
  case token.type
    when :escaped_char
      token.value.sub(/\A\\/, '')
    when :unicode_char
      hex = token.value.sub(/\A\\u/, '')
      [hex.to_i(16)].pack('U*')
    when :escaped_backslash
      '\\'
    when :quoted_string
      token.value[1..-2]
    when :doubled_quote
      "'"
    else
      token.value
  end
end

Instance Method Details

#backward?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)

113
114
115
116
# File 'lib/twitter_cldr/transforms/rule.rb', line 113

def backward?
  raise NotImplementedError,
    "#{__method__} must be defined in derived classes"
end

#forward?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)

108
109
110
111
# File 'lib/twitter_cldr/transforms/rule.rb', line 108

def forward?
  raise NotImplementedError,
    "#{__method__} must be defined in derived classes"
end

#invertObject

Raises:

  • (NotImplementedError)

118
119
120
121
# File 'lib/twitter_cldr/transforms/rule.rb', line 118

def invert
  raise NotImplementedError,
    "#{__method__} must be defined in derived classes"
end

#is_comment?Boolean

Returns:

  • (Boolean)

104
105
106
# File 'lib/twitter_cldr/transforms/rule.rb', line 104

def is_comment?
  false
end

#is_conversion_rule?Boolean

Returns:

  • (Boolean)

92
93
94
# File 'lib/twitter_cldr/transforms/rule.rb', line 92

def is_conversion_rule?
  false
end

#is_conversion_rule_set?Boolean

Returns:

  • (Boolean)

96
97
98
# File 'lib/twitter_cldr/transforms/rule.rb', line 96

def is_conversion_rule_set?
  false
end

#is_filter_rule?Boolean

Returns:

  • (Boolean)

84
85
86
# File 'lib/twitter_cldr/transforms/rule.rb', line 84

def is_filter_rule?
  false
end

#is_transform_rule?Boolean

Returns:

  • (Boolean)

88
89
90
# File 'lib/twitter_cldr/transforms/rule.rb', line 88

def is_transform_rule?
  false
end

#is_variable?Boolean

Returns:

  • (Boolean)

100
101
102
# File 'lib/twitter_cldr/transforms/rule.rb', line 100

def is_variable?
  false
end

#token_string(tokens) ⇒ Object


80
81
82
# File 'lib/twitter_cldr/transforms/rule.rb', line 80

def token_string(tokens)
  self.class.token_string(tokens)
end

#token_value(token) ⇒ Object


76
77
78
# File 'lib/twitter_cldr/transforms/rule.rb', line 76

def token_value(token)
  self.class.token_value(token)
end