Class: TwitterCldr::Transforms::Conversions::ConversionRule

Inherits:
Rule
  • Object
show all
Extended by:
TwitterCldr::Tokenizers
Defined in:
lib/twitter_cldr/transforms/conversions/conversion_rule.rb

Constant Summary

Constants inherited from Rule

Rule::STRING_TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rule

#is_comment?, #is_conversion_rule_set?, #is_filter_rule?, #is_transform_rule?, #is_variable?, regexp_token_string, remove_comment, replace_symbols, #token_string, token_string, #token_value, token_value

Constructor Details

#initialize(direction, left, right, original_rule_text, index) ⇒ ConversionRule

Returns a new instance of ConversionRule.



49
50
51
52
53
54
55
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 49

def initialize(direction, left, right, original_rule_text, index)
  @direction = direction
  @left = left
  @right = right
  @original_rule_text = original_rule_text
  @index = index
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



46
47
48
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 46

def direction
  @direction
end

#indexObject (readonly)

Returns the value of attribute index.



47
48
49
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 47

def index
  @index
end

#leftObject (readonly)

Returns the value of attribute left.



46
47
48
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 46

def left
  @left
end

#original_rule_textObject (readonly)

Returns the value of attribute original_rule_text.



47
48
49
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 47

def original_rule_text
  @original_rule_text
end

#rightObject (readonly)

Returns the value of attribute right.



46
47
48
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 46

def right
  @right
end

Class Method Details

.accepts?(rule_text) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 24

def accepts?(rule_text)
  !!(rule_text =~ /([^\\]|\A)[<>]{1,2}/)
end

.parse(rule_text, symbol_table, index) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 14

def parse(rule_text, symbol_table, index)
  options = {
    original_rule_text: rule_text,
    index: index
  }

  tokens = tokenize(rule_text, symbol_table)
  parser.parse(tokens, options)
end

Instance Method Details

#backward?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 61

def backward?
  direction == :backward
end

#bidirectional?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 65

def bidirectional?
  direction == :bidirectional
end

#can_invert?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 69

def can_invert?
  bidirectional? || backward?
end

#codepointsObject



90
91
92
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 90

def codepoints
  left.codepoints
end

#cursor_offsetObject



124
125
126
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 124

def cursor_offset
  right.cursor_offset
end

#forward?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 57

def forward?
  direction == :bidirectional || direction == :forward
end

#has_codepoints?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 94

def has_codepoints?
  left.has_codepoints?
end

#invertObject



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 77

def invert
  case direction
    when :backward
      self.class.new(
        :forward, left, right, original_rule_text, index
      )
    else
      self.class.new(
        direction, right, left, original_rule_text, index
      )
  end
end

#is_conversion_rule?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 73

def is_conversion_rule?
  true
end

#match(cursor) ⇒ Object



98
99
100
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 98

def match(cursor)
  left.match(cursor)
end

#originalObject



102
103
104
105
106
107
108
109
110
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 102

def original
  @original ||= begin
    key = left.key.inject('') do |ret, token|
      ret + (token.type == :capture ? '' : token.value)
    end

    left.before_context + key + left.after_context
  end
end

#replacement_for(side_match) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/twitter_cldr/transforms/conversions/conversion_rule.rb', line 112

def replacement_for(side_match)
  right.key.inject('') do |ret, token|
    ret + case token.type
      when :capture
        idx = token.value[1..-1].to_i - 1
        side_match.captures[idx]
      else
        token_value(token)
    end
  end
end