Class: StringDirection::Detector
- Inherits:
-
Object
- Object
- StringDirection::Detector
- Defined in:
- lib/string-direction/detector.rb
Overview
String direction detector
Instance Attribute Summary collapse
-
#strategies ⇒ Array
Array of initialized strategies used, in order, to try to detect the direction of a string.
Instance Method Summary collapse
-
#bidi?(string) ⇒ Boolean
Returns whether string is bidirectional or not.
-
#direction(string) ⇒ String?
Tries to detect and return the direction of a string.
-
#initialize(*strategies) ⇒ Detector
constructor
Initialize strategies from given arguments.
-
#ltr?(string) ⇒ Boolean
Returns whether string is left-to-right or not.
-
#rtl?(string) ⇒ Boolean
Returns whether string is right-to-left or not.
Constructor Details
#initialize(*strategies) ⇒ Detector
Initialize strategies from given arguments. If no strategies are given, they are taken from the value of Configuration#default_strategies
12 13 14 15 |
# File 'lib/string-direction/detector.rb', line 12 def initialize(*strategies) strategies = StringDirection.configuration.default_strategies if strategies.empty? initialize_strategies(strategies) end |
Instance Attribute Details
#strategies ⇒ Array
Array of initialized strategies used, in order, to try to detect the direction of a string
7 8 9 |
# File 'lib/string-direction/detector.rb', line 7 def strategies @strategies end |
Instance Method Details
#bidi?(string) ⇒ Boolean
Returns whether string is bidirectional or not
50 51 52 |
# File 'lib/string-direction/detector.rb', line 50 def bidi?(string) direction(string) == bidi end |
#direction(string) ⇒ String?
Tries to detect and return the direction of a string. It returns ltr if the string is left-to-right, rtl if it is right-to-left, bidi if it is bidirectional or nil if it can't detect the direction. It iterates through #strategies until one of them successes.
21 22 23 24 25 26 27 28 |
# File 'lib/string-direction/detector.rb', line 21 def direction(string) direction = nil strategies.each do |strategy| direction = strategy.run(string) break if direction end direction end |
#ltr?(string) ⇒ Boolean
Returns whether string is left-to-right or not
34 35 36 |
# File 'lib/string-direction/detector.rb', line 34 def ltr?(string) direction(string) == ltr end |
#rtl?(string) ⇒ Boolean
Returns whether string is right-to-left or not
42 43 44 |
# File 'lib/string-direction/detector.rb', line 42 def rtl?(string) direction(string) == rtl end |