Class: I18n::Index::Key

Inherits:
Object show all
Defined in:
lib/i18n/index/key.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Key

Returns a new instance of Key.



20
21
22
# File 'lib/i18n/index/key.rb', line 20

def initialize(key)
  self.key = key
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



18
19
20
# File 'lib/i18n/index/key.rb', line 18

def key
  @key
end

Class Method Details

.pattern(key) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/i18n/index/key.rb', line 9

def pattern(key)
  key = key.to_s.dup
  match_start = key.gsub!(/^\*/, '') ? '' : '^'
  match_end = key.gsub!(/\*$/, '') ? '' : '$'
  pattern = Regexp.escape("#{key}")
  /#{match_start}#{pattern}#{match_end}/
end

.patterns(*keys) ⇒ Object



5
6
7
# File 'lib/i18n/index/key.rb', line 5

def patterns(*keys)
  keys.inject({}) { |result, key| result[key] = pattern(key); result }
end

Instance Method Details

#<=>(other) ⇒ Object



31
32
33
# File 'lib/i18n/index/key.rb', line 31

def <=>(other)
  key <=> other.key
end

#==(other) ⇒ Object Also known as: eql?



35
36
37
# File 'lib/i18n/index/key.rb', line 35

def ==(other)
  key == (other.respond_to?(:key) ? other.key : other)
end

#hashObject



40
41
42
# File 'lib/i18n/index/key.rb', line 40

def hash
  key.hash
end

#matches?(*keys) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
# File 'lib/i18n/index/key.rb', line 24

def matches?(*keys)
  patterns = Key.patterns(*keys)
  patterns.empty? || patterns.any? do |key, pattern|
    self.key == key.to_sym || self.key.to_s =~ pattern
  end
end