Class: Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/uppercrust/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name, data, base_only) ⇒ Parser

Returns a new instance of Parser.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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

Instance Attribute Details

#file_nameObject

Returns the value of attribute file_name.



5
6
7
# File 'lib/uppercrust/parser.rb', line 5

def file_name
  @file_name
end

Instance Method Details

#copy_type(type) ⇒ Object



142
143
144
# File 'lib/uppercrust/parser.rb', line 142

def copy_type(type)
  type != 'NSInteger' && type != 'BOOL'
end

#extends_class(data) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# 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) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# 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, readonly) _#{extends} #{self.copy_type(type) ? '*' : ''}#{name};"
    elsif type == 'NSString' && type_info['format'] == 'date-time'
      extracted_properties << "@property(strong, nonatomic, readonly) NSDate *#{name};"
    else
      extracted_properties << "@property(#{self.copy_type(type) ? 'copy' : 'assign'}, nonatomic, readonly) #{type} #{self.copy_type(type) ? '*' : ''}#{name};"
    end
  end

  extracted_properties
end

#extract_variable_names(keys) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
# 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_filesObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object



88
89
90
91
92
93
94
95
# 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) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# 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) ⇒ Object



51
52
53
# 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) ⇒ Object



167
168
169
170
# File 'lib/uppercrust/parser.rb', line 167

def snake_to_camel(file_name)
    prefix = "WP"
  (file_name.split('_').length > 1) ? file_name.split('_').map { |w| w.capitalize }.join('') : prefix+file_name.capitalize
end