Class: TwitterCldr::DataReaders::AdditionalDateFormatSelector

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_cldr/data_readers/additional_date_format_selector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern_hash) ⇒ AdditionalDateFormatSelector

Returns a new instance of AdditionalDateFormatSelector.



11
12
13
# File 'lib/twitter_cldr/data_readers/additional_date_format_selector.rb', line 11

def initialize(pattern_hash)
  @pattern_hash = pattern_hash
end

Instance Attribute Details

#pattern_hashObject (readonly)

Returns the value of attribute pattern_hash.



9
10
11
# File 'lib/twitter_cldr/data_readers/additional_date_format_selector.rb', line 9

def pattern_hash
  @pattern_hash
end

Instance Method Details

#find_closest(goal_pattern) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/twitter_cldr/data_readers/additional_date_format_selector.rb', line 15

def find_closest(goal_pattern)
  if !goal_pattern || goal_pattern.strip.empty?
    nil
  else
    cache_key = TwitterCldr::Utils.compute_cache_key(goal_pattern)
    pattern_cache[cache_key] ||= if @pattern_hash.include?(goal_pattern.to_sym)
      goal_pattern.to_sym
    else
      rank(goal_pattern).min do |(p1, score1), (p2, score2)|
        score1 <=> score2
      end.first.to_sym
    end

    @pattern_hash[pattern_cache[cache_key]]
  end
end

#patternsObject



32
33
34
# File 'lib/twitter_cldr/data_readers/additional_date_format_selector.rb', line 32

def patterns
  @pattern_hash.keys.map(&:to_s)
end