class ErrorPlistGenerator
Attributes
Public Class Methods
# 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
Public Instance Methods
Check the input
# 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
Generic NSError+ErrorCode.h and NSError+ErrorCode.m
# 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
Generic NSError+ErrorDomain.h and NSError+ErrorDomain.m
# 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 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.
# 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_user_info| 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 error key out. It will serve as a reference when we generate numeric key for textual error keys.
Returns: extracted numeric keys from plist
# 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_user_info| 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 NSError+ErrorDomain files
# 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 the NSError+ErrorCode files
# 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
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
# 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