Class: Parser
- Inherits:
-
Object
- Object
- Parser
- Defined in:
- lib/uppercrust/parser.rb
Instance Attribute Summary collapse
-
#file_name ⇒ Object
Returns the value of attribute file_name.
Instance Method Summary collapse
- #copy_type(type) ⇒ Object
- #extends_class(data) ⇒ Object
- #extract_properties(properties) ⇒ Object
- #extract_variable_names(keys) ⇒ Object
- #generate_files ⇒ Object
- #get_array_converters(properties) ⇒ Object
- #get_contains(properties) ⇒ Object
- #get_import(data) ⇒ Object
-
#initialize(file_name, data, base_only) ⇒ Parser
constructor
A new instance of Parser.
- #match_type(in_type) ⇒ Object
- #read_template(template_name) ⇒ Object
- #snake_to_camel(file_name) ⇒ Object
Constructor Details
#initialize(file_name, data, base_only) ⇒ Parser
Returns a new instance of Parser.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/uppercrust/parser.rb', line 5 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 |
Instance Attribute Details
#file_name ⇒ Object
Returns the value of attribute file_name.
3 4 5 |
# File 'lib/uppercrust/parser.rb', line 3 def file_name @file_name end |
Instance Method Details
#copy_type(type) ⇒ Object
123 124 125 |
# File 'lib/uppercrust/parser.rb', line 123 def copy_type(type) type != 'NSInteger' && type != 'BOOL' end |
#extends_class(data) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/uppercrust/parser.rb', line 86 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) ⇒ Object
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/uppercrust/parser.rb', line 112 def extract_properties(properties) extracted_properties = [] properties.each do |name, type_info| type = match_type(type_info['type']) extracted_properties << "@property(#{self.copy_type(type) ? 'copy' : 'assign'}, nonatomic, readonly) #{type} #{self.copy_type(type) ? '*' : ''}#{name};" end extracted_properties end |
#extract_variable_names(keys) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/uppercrust/parser.rb', line 98 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 ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/uppercrust/parser.rb', line 20 def generate_files output = {} output["_#{@generated_class}.h"] = Mustache.render(read_template('base_header'), :class_name => @generated_class, :extends_class => @extends_class, :import => @import, :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?, :contains => @contains_classes, :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) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/uppercrust/parser.rb', line 64 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 return [NSValueTransformer mtl_JSONDictionaryTransformerWithModelClass:_#{extends}.class];\n}" end end array_converters end |
#get_contains(properties) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/uppercrust/parser.rb', line 52 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 end contains end |
#get_import(data) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/uppercrust/parser.rb', line 77 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) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/uppercrust/parser.rb', line 127 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) ⇒ Object
48 49 50 |
# File 'lib/uppercrust/parser.rb', line 48 def read_template(template_name) File.read("#{File.dirname(__FILE__)}/tpl/#{template_name}.mustache") end |
#snake_to_camel(file_name) ⇒ Object
148 149 150 |
# File 'lib/uppercrust/parser.rb', line 148 def snake_to_camel(file_name) (file_name.split('_').length > 1) ? file_name.split('_').map { |w| w.capitalize }.join('') : file_name.capitalize end |