Class: TwitterCldr::Transforms::Conversions::Side

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(before_context, key, after_context, cursor_offset) ⇒ Side

Returns a new instance of Side.


24
25
26
27
28
29
# File 'lib/twitter_cldr/transforms/conversions/side.rb', line 24

def initialize(before_context, key, after_context, cursor_offset)
  @before_context = before_context
  @key = key
  @after_context = after_context
  @cursor_offset = cursor_offset
end

Instance Attribute Details

#after_contextObject (readonly)

Returns the value of attribute after_context.


22
23
24
# File 'lib/twitter_cldr/transforms/conversions/side.rb', line 22

def after_context
  @after_context
end

#before_contextObject (readonly)

Returns the value of attribute before_context.


21
22
23
# File 'lib/twitter_cldr/transforms/conversions/side.rb', line 21

def before_context
  @before_context
end

#cursor_offsetObject (readonly)

Returns the value of attribute cursor_offset.


22
23
24
# File 'lib/twitter_cldr/transforms/conversions/side.rb', line 22

def cursor_offset
  @cursor_offset
end

#keyObject (readonly)

Returns the value of attribute key.


21
22
23
# File 'lib/twitter_cldr/transforms/conversions/side.rb', line 21

def key
  @key
end

Instance Method Details

#codepointsObject


79
80
81
82
83
84
85
# File 'lib/twitter_cldr/transforms/conversions/side.rb', line 79

def codepoints
  if first_elem = key_u_regexp.elements.first
    first_elem.codepoints
  else
    []
  end
end

#has_codepoints?Boolean

Returns:

  • (Boolean)

70
71
72
73
74
75
76
77
# File 'lib/twitter_cldr/transforms/conversions/side.rb', line 70

def has_codepoints?
  if first_elem = key_u_regexp.elements.first
    first_elem.respond_to?(:codepoints) &&
      !first_elem.codepoints.empty?
  else
    false
  end
end

#match(cursor) ⇒ Object


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

def match(cursor)
  if before_match = match_before(cursor)
    if key_match = match_key(cursor, before_match)
      if after_match = match_after(cursor, key_match)
        SideMatch.new(
          before_match.offset(0),
          key_match.offset(0),
          after_match.offset(0),
          before_match.captures +
            key_match.captures +
            after_match.captures
        )
      end
    end
  end
end

#match_after(cursor, key_match) ⇒ Object


64
65
66
67
68
# File 'lib/twitter_cldr/transforms/conversions/side.rb', line 64

def match_after(cursor, key_match)
  if match = after_context_regexp.match(cursor.text, key_match.end(0))
    match if match.begin(0) == key_match.end(0)
  end
end

#match_before(cursor) ⇒ Object


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

def match_before(cursor)
  cursor.text.scan(before_context_regexp) do
    match = Regexp.last_match
    if match.end(0) >= cursor.position && match.begin(0) <= cursor.position
      return match
    end
  end
  nil
end

#match_key(cursor, before_match) ⇒ Object


58
59
60
61
62
# File 'lib/twitter_cldr/transforms/conversions/side.rb', line 58

def match_key(cursor, before_match)
  if match = key_regexp.match(cursor.text, before_match.end(0))
    match if match.begin(0) == before_match.end(0)
  end
end