class StringDirection::Detector

String direction detector

Attributes

strategies[RW]

Array of initialized strategies used, in order, to try to detect the direction of a string

@return [Array]

Public Class Methods

new(*strategies) click to toggle source

Initialize strategies from given arguments. If no strategies are given, they are taken from the value of {StringDirection::Configuration#default_strategies}

@raise [ArgumentError] if strategy class is not found. For example, for an strategy `:marks` a class `StringDirection::MarksStrategy` is expected

# File lib/string-direction/detector.rb, line 12
def initialize(*strategies)
  strategies = StringDirection.configuration.default_strategies if strategies.empty?
  initialize_strategies(strategies)
end

Public Instance Methods

bidi?(string) click to toggle source

Returns whether string is bidirectional or not

@param string [String] The string to inspect @return [Boolean]

# File lib/string-direction/detector.rb, line 50
def bidi?(string)
  direction(string) == bidi
end
direction(string) click to toggle source

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.

@param string [String] The string to inspect @return [String, nil]

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

Returns whether string is left-to-right or not

@param string [String] The string to inspect @return [Boolean]

# File lib/string-direction/detector.rb, line 34
def ltr?(string)
  direction(string) == ltr
end
rtl?(string) click to toggle source

Returns whether string is right-to-left or not

@param string [String] The string to inspect @return [Boolean]

# File lib/string-direction/detector.rb, line 42
def rtl?(string)
  direction(string) == rtl
end

Private Instance Methods

bidi() click to toggle source
# File lib/string-direction/detector.rb, line 80
def bidi
  StringDirection::BIDI
end
infer_strategy_class_name(strategy) click to toggle source
# File lib/string-direction/detector.rb, line 67
def infer_strategy_class_name(strategy)
  base_name = strategy.to_s.split('_').map(&:capitalize).join
  "StringDirection::#{base_name}Strategy"
end
initialize_strategies(strategies) click to toggle source
# File lib/string-direction/detector.rb, line 56
def initialize_strategies(strategies)
  self.strategies = strategies.map do |strategy|
    begin
      name = infer_strategy_class_name(strategy)
      Kernel.const_get(name).new
    rescue NameError
      raise ArgumentError, "Can't find '#{name}' strategy"
    end
  end
end
ltr() click to toggle source
# File lib/string-direction/detector.rb, line 72
def ltr
  StringDirection::LTR
end
rtl() click to toggle source
# File lib/string-direction/detector.rb, line 76
def rtl
  StringDirection::RTL
end