Class: ErrorPlistGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/KMQToolKitGem/error_plist_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plist_hash, prefix, base_path) ⇒ ErrorPlistGenerator

Returns a new instance of ErrorPlistGenerator.



7
8
9
10
11
12
13
# File 'lib/KMQToolKitGem/error_plist_generator.rb', line 7

def initialize(plist_hash, prefix, base_path)
  @template_folder = File.expand_path('../../templates', __FILE__)
  @plist_hash = plist_hash
  @prefix = prefix
  @base_path = base_path
  check
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



5
6
7
# File 'lib/KMQToolKitGem/error_plist_generator.rb', line 5

def base_path
  @base_path
end

#plist_hashObject (readonly)

Returns the value of attribute plist_hash.



5
6
7
# File 'lib/KMQToolKitGem/error_plist_generator.rb', line 5

def plist_hash
  @plist_hash
end

#prefixObject (readonly)

Returns the value of attribute prefix.



5
6
7
# File 'lib/KMQToolKitGem/error_plist_generator.rb', line 5

def prefix
  @prefix
end

Instance Method Details

#checkObject

Check the input



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/KMQToolKitGem/error_plist_generator.rb', line 16

def check
  if !@plist_hash.is_a?(Hash)
    raise RuntimeError, 'Invalid plist format'
  end

  @plist_hash.each do |domain_key, errors_hash|
    if !errors_hash.is_a?(Hash)
      raise RuntimeError, 'Invalid plist format'
    end
  end
end

#create_error_code_file(codes, type) ⇒ Object

Generic NSError+ErrorCode.h and NSError+ErrorCode.m



41
42
43
44
45
46
47
# File 'lib/KMQToolKitGem/error_plist_generator.rb', line 41

def create_error_code_file(codes, type)
  file_name = "NSError+#{@prefix}ErrorCode.#{type}"
  template_path = "#{@template_folder}/NSError+ErrorCode.#{type}.erb"
  absolute_file_path = "#{@base_path}/#{file_name}"
  variables = OpenStruct.new(codes: codes, prefix: @prefix)
  Helper.write_to_file(absolute_file_path, template_path, variables.instance_eval{ binding })
end

#create_error_domain_file(domains, type) ⇒ Object

Generic NSError+ErrorDomain.h and NSError+ErrorDomain.m



57
58
59
60
61
62
63
# File 'lib/KMQToolKitGem/error_plist_generator.rb', line 57

def create_error_domain_file(domains, type)
  file_name = "NSError+#{@prefix}ErrorDomain.#{type}"
  template_path = "#{@template_folder}/NSError+ErrorDomain.#{type}.erb"
  absolute_file_path = "#{@base_path}/#{file_name}"
  variables = OpenStruct.new(domains: domains, prefix: @prefix)
  Helper.write_to_file(absolute_file_path, template_path, variables.instance_eval{ binding })
end

#extract_and_allocate_text_keys_to_numeric(numeric_keys) ⇒ Object

Extract the textual keys from input and assign them numeric values, using the already extacted numeric error keys as a reference. The assigned numeric keys shall be greater than the minimum extracted numeric key and does not collide with already used numeric key

Returns: A hash keyed by the textual key and an array containing error domain and generated numeric key as value.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/KMQToolKitGem/error_plist_generator.rb', line 89

def extract_and_allocate_text_keys_to_numeric(numeric_keys)
  codes = {}
  @plist_hash.each do |domain_key, errors_hash|
    text_keys = []
    errors_hash.each do |error_key, |
      error_key_name = error_key.split(":")[0]
      if !Helper.is_integer?(error_key_name)
        text_keys << error_key_name
      end
    end
    text_keys.each do |text_key|
      code = next_available_numeric numeric_keys
      codes[text_key] = [domain_key, code]
    end
  end
  return codes
end

#extract_numeric_keysObject

Extract numeric error key out. It will serve as a reference when we generate numeric key for textual error keys.

Returns: extracted numeric keys from plist



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/KMQToolKitGem/error_plist_generator.rb', line 69

def extract_numeric_keys
  numeric_keys = []
  @plist_hash.each do |domain_key, errors_hash|
    errors_hash.each do |error_key, |
      error_key_name = error_key.split(":")[0]
      if Helper.is_integer? error_key_name
        numeric_keys << error_key_name.to_i
      end
    end
  end
  return numeric_keys
end

#generate_domainObject

Generate NSError+ErrorDomain files



50
51
52
53
54
# File 'lib/KMQToolKitGem/error_plist_generator.rb', line 50

def generate_domain
  domains = @plist_hash.keys
  create_error_domain_file domains, 'h'
  create_error_domain_file domains, 'm'
end

#generate_error_codeObject

Generate the NSError+ErrorCode files



29
30
31
32
33
34
35
36
37
38
# File 'lib/KMQToolKitGem/error_plist_generator.rb', line 29

def generate_error_code
  numeric_keys = extract_numeric_keys
  if numeric_keys.empty?
    numeric_keys << 99
  end

  codes = extract_and_allocate_text_keys_to_numeric numeric_keys
  create_error_code_file codes, 'h'
  create_error_code_file codes, 'm'
end

#next_available_numeric(numeric_keys) ⇒ Object

Find the next availblae numeric given a numeric array. The found numeric shall be greater than the minimum and does not collide with existing numeric.

Return: the next available numeric



111
112
113
114
115
116
117
118
# File 'lib/KMQToolKitGem/error_plist_generator.rb', line 111

def next_available_numeric(numeric_keys)
  i = numeric_keys.min
  while numeric_keys.include?(i) do
    i += 1
  end
  numeric_keys << i
  return i
end