README.md in string-direction-1.0.0 vs README.md in string-direction-1.1.0
- old
+ new
@@ -29,23 +29,33 @@
## Strategies
`string-direction` uses different strategies in order to try to detect the direction of a string. The detector uses them once at a time and returns the result once one of them succeeds, aborting any further analysis.
-Right now, two strategies are natively integrated: `marks` and `characters`. They are used, in that order, as default strategies if no strategies are given.
+Strategies are passed to the detector during its initialization:
+```ruby
+detector = StringDirection::Detector.new(:foo, :bar)
+```
+
+In the above example, classes `StringDirection::FooStrategy` and `StringDirection::BarStrategy` have to be in the load path.
+
+Two strategies are natively integrated: `marks` and `characters`. They are used, in that order, as default strategies if no arguments are given to the detector.
+
### marks
-Looks for the presence of direction Unicode marks: [left-to-right](http://en.wikipedia.org/wiki/Left-to-right_mark) (\u200e) or [right-to-left](http://en.wikipedia.org/wiki/Right-to-left_mark) (\u200f).
+Looks for the presence of Unicode direction marks: [left-to-right](http://en.wikipedia.org/wiki/Left-to-right_mark) (\u200e) or [right-to-left](http://en.wikipedia.org/wiki/Right-to-left_mark) (\u200f).
```ruby
detector = StringDirection::Detector.new(:marks)
detector.direction("\u200eالعربية") #=> "ltr"
detector.direction("\u200fEnglish") #=> "rtl"
```
+`marks` strategy can not only analyze a string but everything responding to `to_s`.
+
### characters
Looks for the presence of right-to-left characters in the scripts used in the string.
By default, `string-direction` consider following scripts to have a right-to-left writing:
@@ -69,12 +79,12 @@
You can change these defaults:
```ruby
detector.direction('ᚪᚫᚬᚭᚮᚯ') #=> 'ltr'
-StringDirection.configuration do |config|
- config.rtl_scripts << 'Runic'
+StringDirection.configure do |config|
+ config.rtl_scripts << 'Runic'
end
detector.direction('ᚪᚫᚬᚭᚮᚯ') #=> 'rtl'
```
@@ -88,16 +98,18 @@
* Runic
* Ugaritic
Keep in mind than only [scripts recognized by Ruby regular expressions](http://www.ruby-doc.org/core-1.9.3/Regexp.html#label-Character+Properties) are allowed.
+`characters` strategy can not only analyze a string but everything responding to `to_s`.
+
### Custom Strategies
-You can define your custom strategies. To do so, you just have to define a class inside `StringDirection` module with a name ending with `Strategy`. This class has to respond to an instance method `run` which takes the string as argument. You can inherit from `StringDirection::Strategy` to have convenient methods `ltr`, `rtl` and `bidi`.
+You can define your custom strategies. To do so, you just have to define a class inside `StringDirection` module with a name ending with `Strategy`. This class has to respond to an instance method `run` which takes the string as argument. You can inherit from `StringDirection::Strategy` to have convenient methods `ltr`, `rtl` and `bidi` which return expected result. If the strategy doesn't know the direction, it must return `nil`.
```ruby
-class StringDirection::AlwaysLtrStrategy
+class StringDirection::AlwaysLtrStrategy < StringDirection::Strategy
def run(string)
ltr
end
end
@@ -108,11 +120,29 @@
### Changing default strategies
`marks` and `characters` are default strategies, but you can change them:
```ruby
-StringDirection.configuration do |config|
- config.default_strategies = [:custom, :marks, :always_ltr]
+StringDirection.configure do |config|
+ config.default_strategies = [:custom, :marks, :always_ltr]
+end
+```
+
+## Monkey patching String
+
+If you desire, you can monkey patch `String`:
+
+```ruby
+String.send(:include, StringDirection::StringMethods)
+
+'english'.direction #=> 'ltr'
+```
+
+In that case, strategies configured in `string_method_strategies` are used:
+
+```ruby
+StringDirection.configure do |config|
+ config.string_methods_strategies = [:marks, :characters]
end
```
## Release Policy