Class: ICU::Collation::Collator
- Inherits:
-
Object
- Object
- ICU::Collation::Collator
- Defined in:
- lib/ffi-icu/collation.rb
Constant Summary collapse
- ULOC_VALID_LOCALE =
1
Instance Method Summary collapse
- #collate(sortable) ⇒ Object
- #compare(a, b) ⇒ Object
- #equal?(*args) ⇒ Boolean
- #greater?(a, b) ⇒ Boolean
- #greater_or_equal?(a, b) ⇒ Boolean
-
#initialize(locale) ⇒ Collator
constructor
A new instance of Collator.
- #locale ⇒ Object
- #rules ⇒ Object
Constructor Details
#initialize(locale) ⇒ Collator
Returns a new instance of Collator.
32 33 34 35 |
# File 'lib/ffi-icu/collation.rb', line 32 def initialize(locale) ptr = Lib.check_error { |error| Lib.ucol_open(locale, error) } @c = FFI::AutoPointer.new(ptr, Lib.method(:ucol_close)) end |
Instance Method Details
#collate(sortable) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/ffi-icu/collation.rb', line 72 def collate(sortable) unless sortable.respond_to?(:sort) raise ArgumentError, "argument must respond to :sort with arity of 2" end sortable.sort { |a, b| compare a, b } end |
#compare(a, b) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/ffi-icu/collation.rb', line 41 def compare(a, b) Lib.ucol_strcoll( @c, UCharPointer.from_string(a), a.jlength, UCharPointer.from_string(b), b.jlength ) end |
#equal?(*args) ⇒ Boolean
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ffi-icu/collation.rb', line 59 def equal?(*args) return super() if args.empty? if args.size != 2 raise ArgumentError, "wrong number of arguments (#{args.size} for 2)" end a, b = args Lib.ucol_equal(@c, UCharPointer.from_string(a), a.jlength, UCharPointer.from_string(b), b.jlength) end |
#greater?(a, b) ⇒ Boolean
49 50 51 52 |
# File 'lib/ffi-icu/collation.rb', line 49 def greater?(a, b) Lib.ucol_greater(@c, UCharPointer.from_string(a), a.jlength, UCharPointer.from_string(b), b.jlength) end |
#greater_or_equal?(a, b) ⇒ Boolean
54 55 56 57 |
# File 'lib/ffi-icu/collation.rb', line 54 def greater_or_equal?(a, b) Lib.ucol_greaterOrEqual(@c, UCharPointer.from_string(a), a.jlength, UCharPointer.from_string(b), b.jlength) end |
#locale ⇒ Object
37 38 39 |
# File 'lib/ffi-icu/collation.rb', line 37 def locale Lib.check_error { |error| Lib.ucol_getLocale(@c, ULOC_VALID_LOCALE, error) } end |