class TwitterCldr::Transforms::Rule

Base class for all transform rules

Constants

STRING_TYPES

Public Class Methods

regexp_token_string(tokens) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Public Instance Methods

backward?() click to toggle source
# File lib/twitter_cldr/transforms/rule.rb, line 113
def backward?
  raise NotImplementedError,
    "#{__method__} must be defined in derived classes"
end
forward?() click to toggle source
# File lib/twitter_cldr/transforms/rule.rb, line 108
def forward?
  raise NotImplementedError,
    "#{__method__} must be defined in derived classes"
end
invert() click to toggle source
# File lib/twitter_cldr/transforms/rule.rb, line 118
def invert
  raise NotImplementedError,
    "#{__method__} must be defined in derived classes"
end
is_comment?() click to toggle source
# File lib/twitter_cldr/transforms/rule.rb, line 104
def is_comment?
  false
end
is_conversion_rule?() click to toggle source
# File lib/twitter_cldr/transforms/rule.rb, line 92
def is_conversion_rule?
  false
end
is_conversion_rule_set?() click to toggle source
# File lib/twitter_cldr/transforms/rule.rb, line 96
def is_conversion_rule_set?
  false
end
is_filter_rule?() click to toggle source
# File lib/twitter_cldr/transforms/rule.rb, line 84
def is_filter_rule?
  false
end
is_transform_rule?() click to toggle source
# File lib/twitter_cldr/transforms/rule.rb, line 88
def is_transform_rule?
  false
end
is_variable?() click to toggle source
# File lib/twitter_cldr/transforms/rule.rb, line 100
def is_variable?
  false
end
token_string(tokens) click to toggle source
# File lib/twitter_cldr/transforms/rule.rb, line 80
def token_string(tokens)
  self.class.token_string(tokens)
end
token_value(token) click to toggle source
# File lib/twitter_cldr/transforms/rule.rb, line 76
def token_value(token)
  self.class.token_value(token)
end