class Parser
Attributes
file_name[RW]
Public Class Methods
new(file_name, data, base_only)
click to toggle source
# File lib/uppercrust/parser.rb, line 7 def initialize(file_name, data, base_only) @file_name = file_name @data = data @base_only = base_only @generated_class = self.snake_to_camel(File.basename(file_name, '.json')) @properties = self.extract_properties(data['properties']) @variable_names = self.extract_variable_names(data['properties'].keys) @extends_class = self.extends_class(data) @import = self.get_import(data) @contains_classes = self.get_contains(data['properties']) @array_converters = self.get_array_converters(data['properties']) end
Public Instance Methods
copy_type(type)
click to toggle source
# File lib/uppercrust/parser.rb, line 142 def copy_type(type) type != 'NSInteger' && type != 'BOOL' end
extends_class(data)
click to toggle source
# File lib/uppercrust/parser.rb, line 97 def extends_class(data) if data['extends'].to_s == '' 'MTLModel<MTLJSONSerializing>' else if data['extends'].is_a? String '_' << self.snake_to_camel(Pathname.new(data['extends']).basename('.json').to_s) else '_' << self.snake_to_camel(Pathname.new(data['extends']['$ref']).basename('.json').to_s) end end end
extract_properties(properties)
click to toggle source
# File lib/uppercrust/parser.rb, line 123 def extract_properties(properties) extracted_properties = [] properties.each do |name, type_info| type = match_type(type_info['type']) if type == 'NSObject' && type_info['$ref'] != nil extends = type_info['$ref'] != '#' ? self.snake_to_camel(Pathname.new(type_info['$ref']).basename('.json').to_s) : @generated_class extracted_properties << "@property(#{self.copy_type(type) ? 'copy' : 'assign'}, nonatomic) #{extends} #{self.copy_type(type) ? '*' : ''}#{name};" elsif type == 'NSString' && type_info['format'] == 'date-time' extracted_properties << "@property(strong, nonatomic) NSDate *#{name};" else extracted_properties << "@property(#{self.copy_type(type) ? 'copy' : 'assign'}, nonatomic) #{type} #{self.copy_type(type) ? '*' : ''}#{name};" end end extracted_properties end
extract_variable_names(keys)
click to toggle source
# File lib/uppercrust/parser.rb, line 109 def extract_variable_names(keys) extracted_variables_names = [] if keys.length > 1 last = keys.pop keys.each { |key| extracted_variables_names << "@\"#{key}\" : @\"#{key}\"," } extracted_variables_names << "@\"#{last}\" : @\"#{last}\"" else extracted_variables_names << "@\"#{keys[0]}\" : @\"#{keys[0]}\"" end extracted_variables_names end
generate_files()
click to toggle source
# File lib/uppercrust/parser.rb, line 23 def generate_files output = {} output["_#{@generated_class}.h"] = Mustache.render(read_template('base_header'), :class_name => @generated_class, :extends_class => @extends_class, :import => @import, :contains => @contains_classes, :description => @data['description'], :properties => @properties) output["_#{@generated_class}.m"] = Mustache.render(read_template('base_implementation'), :class_name => @generated_class, :properties => @variable_names, :extends => !@import.nil?, :array_converters => @array_converters) unless @base_only output["#{@generated_class}.h"] = Mustache.render(read_template('header'), :class_name => @generated_class) output["#{@generated_class}.m"] = Mustache.render(read_template('implementation'), :class_name => @generated_class) end output end
get_array_converters(properties)
click to toggle source
# File lib/uppercrust/parser.rb, line 70 def get_array_converters(properties) array_converters = [] properties.each do |name, type_info| if self.match_type(type_info['type']) == 'NSArray' && type_info['items']['$ref'] != nil extends = type_info['items']['$ref'] != '#' ? self.snake_to_camel(Pathname.new(type_info['items']['$ref']).basename('.json').to_s) : @generated_class array_converters << "+ (NSValueTransformer *)#{name}JSONTransformer\n{\n return [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:#{extends}.class];\n}\n" end if self.match_type(type_info['type']) == 'NSObject' && type_info['$ref'] != nil extends = type_info['$ref'] != '#' ? self.snake_to_camel(Pathname.new(type_info['$ref']).basename('.json').to_s) : @generated_class array_converters << "+ (NSValueTransformer *)#{name}JSONTransformer\n{\n return [NSValueTransformer mtl_JSONDictionaryTransformerWithModelClass:#{extends}.class];\n}\n" end end array_converters end
get_contains(properties)
click to toggle source
# File lib/uppercrust/parser.rb, line 55 def get_contains(properties) contains = [] properties.each do |name, type_info| if self.match_type(type_info['type']) == 'NSArray' && type_info['items']['$ref'] != nil && type_info['items']['$ref'] != '#' contains << "#import \"#{self.snake_to_camel(Pathname.new(type_info['items']['$ref']).basename('.json').to_s)}.h\"" end if self.match_type(type_info['type']) == 'NSObject' && type_info['$ref'] != nil && type_info['$ref'] != '#' contains << "#import \"#{self.snake_to_camel(Pathname.new(type_info['$ref']).basename('.json').to_s)}.h\"" end end contains.uniq end
get_import(data)
click to toggle source
# File lib/uppercrust/parser.rb, line 88 def get_import(data) if data['extends'].to_s == '' nil else import = data['extends'].is_a?(String) ? '_' << self.snake_to_camel(data['extends'].sub('.json', '')) : '_' << self.snake_to_camel(File.basename(data['extends']['$ref'], '.json')) "#import \"#{import}.h\"" end end
match_type(in_type)
click to toggle source
# File lib/uppercrust/parser.rb, line 146 def match_type(in_type) case in_type when 'string' 'NSString' when 'number', 'long' 'NSNumber' when 'integer' 'NSInteger' when 'boolean' 'BOOL' when 'object', 'any' 'NSObject' when 'array' 'NSArray' when 'null' 'NSNull' else 'NSObject' end end
read_template(template_name)
click to toggle source
# File lib/uppercrust/parser.rb, line 51 def read_template(template_name) File.read("#{File.dirname(__FILE__)}/tpl/#{template_name}.mustache") end
snake_to_camel(file_name)
click to toggle source
# File lib/uppercrust/parser.rb, line 168 def snake_to_camel(file_name) prefix = "WP" (file_name.split('_').length > 1) ? file_name.split('_').map { |w| w.sub(/^./, &:upcase) }.join('') : prefix+file_name.sub(/^./, &:upcase) end