Class: AdLocalize::Parsers::KeyParser

Inherits:
Object
  • Object
show all
Defined in:
lib/ad_localize/parsers/key_parser.rb

Constant Summary collapse

PLURAL_KEY_REGEXP =
/\#\#\{([A-Za-z]+)\}/.freeze
ADAPTIVE_KEY_REGEXP =
/\#\#\{(\d+)\}/.freeze
INFO_PLIST_KEY_REGEXP =
/(NS.+UsageDescription)|(CF.+Name)|NFCReaderUsageDescription/.freeze

Instance Method Summary collapse

Instance Method Details

#adaptive?(raw_key:) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/ad_localize/parsers/key_parser.rb', line 21

def adaptive?(raw_key:)
  !raw_key.match(ADAPTIVE_KEY_REGEXP).nil?
end

#call(raw_key:) ⇒ Object



10
11
12
13
14
15
# File 'lib/ad_localize/parsers/key_parser.rb', line 10

def call(raw_key:)
  type = compute_type(raw_key: raw_key)
  label = compute_label(raw_key: raw_key, type: type)
  variant_name = compute_variant_name(raw_key: raw_key, type: type)
  Entities::Key.new(id: raw_key, label: label, type: type, variant_name: variant_name)
end

#compute_label(raw_key:, type:) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/ad_localize/parsers/key_parser.rb', line 41

def compute_label(raw_key:, type:)
  case type
  when Entities::WordingType::PLURAL
    raw_key.gsub(PLURAL_KEY_REGEXP, '')
  when Entities::WordingType::ADAPTIVE
    raw_key.gsub(ADAPTIVE_KEY_REGEXP, '')
  else
    raw_key
  end
end

#compute_type(raw_key:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ad_localize/parsers/key_parser.rb', line 29

def compute_type(raw_key:)
  if plural?(raw_key: raw_key)
    Entities::WordingType::PLURAL
  elsif adaptive?(raw_key: raw_key)
    Entities::WordingType::ADAPTIVE
  elsif info_plist?(raw_key: raw_key)
    Entities::WordingType::INFO_PLIST
  else
    Entities::WordingType::SINGULAR
  end
end

#compute_variant_name(raw_key:, type:) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/ad_localize/parsers/key_parser.rb', line 52

def compute_variant_name(raw_key:, type:)
  case type
  when Entities::WordingType::PLURAL
    raw_key.match(PLURAL_KEY_REGEXP)&.captures&.first
  when Entities::WordingType::ADAPTIVE
    raw_key.match(ADAPTIVE_KEY_REGEXP)&.captures&.first
  end
end

#info_plist?(raw_key:) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/ad_localize/parsers/key_parser.rb', line 25

def info_plist?(raw_key:)
  !raw_key.match(INFO_PLIST_KEY_REGEXP).nil?
end

#plural?(raw_key:) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/ad_localize/parsers/key_parser.rb', line 17

def plural?(raw_key:)
  !raw_key.match(PLURAL_KEY_REGEXP).nil?
end