Class: JsonGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/ModelGenerator/JsonGenerator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ JsonGenerator

Returns a new instance of JsonGenerator.



11
12
13
14
# File 'lib/ModelGenerator/JsonGenerator.rb', line 11

def initialize(command)
  @commandTask=command
  @method_generator=MethodGenerator.new(command)
end

Instance Attribute Details

#commandTaskObject (readonly)

Returns the value of attribute commandTask.



8
9
10
# File 'lib/ModelGenerator/JsonGenerator.rb', line 8

def commandTask
  @commandTask
end

#method_generatorObject (readonly)

Returns the value of attribute method_generator.



9
10
11
# File 'lib/ModelGenerator/JsonGenerator.rb', line 9

def method_generator
  @method_generator
end

Instance Method Details

#generate_formaterObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ModelGenerator/JsonGenerator.rb', line 59

def generate_formater
    formater_content=""
    @commandTask.property_name_type_hash.each do |key , value|
        json_column=@commandTask.property_name_json_hash[key]
        json_type=@commandTask.json_name_type_hash[json_column]
        json_format=@commandTask.json_name_format_hash[json_column]
        property_format=@commandTask.property_name_format_hash[key]

        formater_method_content= generate_formater_method_content(key,value,property_format,json_type,json_format)
        if formater_method_content !=nil && json_column
          has_super=@commandTask.parent_class
          formater_content << @method_generator.generate_method("+","NSValueTransformer *" , "#{key}AtJSONTransformer","#{formater_method_content}",has_super)
        end
    end

    return formater_content
end

#generate_formater_method_content(property_name, property_type, property_format, json_type, json_formate) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ModelGenerator/JsonGenerator.rb', line 77

def generate_formater_method_content(property_name,property_type,property_format,json_type,json_formate)

    content=""

    if property_type=="url" && json_type=="string"
        content << "return [NSValueTransformer valueTransformerForName:MTLURLValueTransformerName];\n"
    elsif (property_type=="bool" && json_type=="int")
        content << "return [NSValueTransformer valueTransformerForName:MTLBooleanValueTransformerName];\n"
    elsif property_type != json_type || (property_type == json_type  && property_format != json_formate)
        content = %Q/return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(#{CommonParam.type_mapping[json_type]} x) {
                              return #{FormatTransformer.transform(property_type,property_format,json_type,json_formate)}
                            } reverseBlock:^(#{CommonParam.type_mapping[property_type]} x) {
                              return #{FormatTransformer.transform(json_type,json_formate,property_type,property_format)}
                            }];/
    elsif property_type=="mapping" && json_type=="mapping"
        content = %Q/return [NSValueTransformer mtl_valueMappingTransformerWithDictionary:@{
                             <#dictionary_content#>
                           }];/

    elsif !CommonParam.type_mapping[property_type]
        content << "return [NSValueTransformer mtl_JSONDictionaryTransformerWithModelClass:#{property_name}.class];"
    elsif property_type=="custom"
          content = %Q/return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSString *str) {
                          return <#ConvertFormat#>;
                      } reverseBlock:^(NSDate *date) {
                          return <#ReverseConvertFormat#>;
                      }];/
    else
      content=nil
    end
    return content
end

#generate_json_column_mappingObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ModelGenerator/JsonGenerator.rb', line 16

def generate_json_column_mapping
  json_column_mappings_content=""
  @commandTask.property_name_json_hash.keys.each do |name|
      column=@commandTask.property_name_json_hash[name]
      if(column)
        cap_name = name.slice(0,1).capitalize + name.slice(1..-1)
        json_column_mappings_content << "static NSString * #{cap_name}JsonKey = @\"#{column}\";\n"
      end
  end
  if json_column_mappings_content.length > 0
      annotation = AnnotationGenerator.generate_single_annotation("json column declare")
      json_column_mappings_content = annotation + json_column_mappings_content
  end
  return json_column_mappings_content
end

#generate_property_mappingObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ModelGenerator/JsonGenerator.rb', line 32

def generate_property_mapping
    hasJsonColumn=false
    long_space="           "
    property_mapping_content="return @{\n"
    @commandTask.property_name_json_hash.each do |key , value|
        if value
          hasJsonColumn=true
          cap_key = key.slice(0,1).capitalize + key.slice(1..-1)
          property_mapping_content << "#{long_space} @\"#{key}\":#{cap_key}JsonKey,\n"
        end
    end

    property_mapping_content=property_mapping_content.chomp(",\n")
    property_mapping_content << "\n"
    property_mapping_content << "#{long_space}};"

    if hasJsonColumn
      has_super=@commandTask.parent_class
      content =@method_generator.generate_method("+","NSDictionary *","JSONKeyPathsByPropertyKey","#{property_mapping_content}",has_super)
      annotation = AnnotationGenerator.generate_mark_annotation("json method")
      content = annotation + content
      return content
    else
      return ""
    end
end