File: shared_methods.rb

package info (click to toggle)
ruby-regexp-property-values 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 164 kB
  • sloc: ruby: 243; ansic: 51; makefile: 6; sh: 4
file content (63 lines) | stat: -rw-r--r-- 1,422 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module RegexpPropertyValues
  class Value
    module SharedMethods
      attr_reader :name

      def initialize(name)
        @name = name
      end

      def supported_by_current_ruby?
        !!regexp rescue false
      end

      def ==(other)
        identifier == other.identifier
      end
      alias eql? ==

      def hash
        @hash ||= identifier.hash
      end

      def identifier
        @identifier ||= name.to_s.downcase.gsub(/[^0-9a-z=.]/, '')
      end
      alias to_s identifier

      def full_name
        (original = find_original) ? original.name : raise_unknown_error
      end

      def character_set
        require 'character_set'
        CharacterSet.from_ranges(*matched_ranges)
      end

      private

      def regexp
        @regexp ||= /\p{#{identifier}}/u
      rescue RegexpError, SyntaxError
        raise_unsupported_or_unknown_error
      end

      def find_original
        RegexpPropertyValues.all.find { |orig| orig.eql?(self) } ||
          RegexpPropertyValues.alias_hash[self]
      end

      def raise_unsupported_or_unknown_error
        find_original ? raise_unsupported_error : raise_unknown_error
      end

      def raise_unsupported_error
        raise Error, "Property name `#{name}` is known, but not in this Ruby"
      end

      def raise_unknown_error
        raise Error, "Property name `#{name}` is not known in any Ruby"
      end
    end
  end
end