Class: Pod::Command::Bin::Model

Inherits:
Pod::Command::Bin show all
Defined in:
lib/cocoapods-lhj-bin/command/bin/model.rb

Instance Method Summary collapse

Methods included from CBin::SpecFilesHelper

#binary_spec, #binary_spec_files, #binary_template_spec, #binary_template_spec_file, #binary_template_spec_files, #clear_binary_spec_file_if_needed, #code_spec, #code_spec_files, #create_binary_spec_file, #find_spec_file, #spec_files

Methods included from CBin::SourcesHelper

#binary_source, #code_source, #sources_manager, #sources_option, #valid_sources

Constructor Details

#initialize(argv) ⇒ Model

Returns a new instance of Model.



12
13
14
15
16
# File 'lib/cocoapods-lhj-bin/command/bin/model.rb', line 12

def initialize(argv)
  @url = argv.shift_argument
  @model_pre_name = argv.option('model-pre', 'ML')
  @models = []
end

Instance Method Details

#fetch_modelObject



39
40
41
42
43
44
# File 'lib/cocoapods-lhj-bin/command/bin/model.rb', line 39

def fetch_model
  uri = URI.parse(@url)
  res = Net::HTTP.get_response(uri)
  res_body = JSON.parse(res.body)
  res_body['detailMsg']
end

#fetch_models(name, obj) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cocoapods-lhj-bin/command/bin/model.rb', line 50

def fetch_models(name, obj)
  model = obj
  model = obj.first if obj.respond_to? :<<
  @models.unshift({name: name, value: model}) if model.instance_of? Hash
  if model.instance_of? Hash
    model.each do |key, value|
      if (value.instance_of? Hash) || (value.instance_of? Array)
        fetch_models(key, value)
      end
    end
  end
end

#gen_modelObject



32
33
34
35
36
37
# File 'lib/cocoapods-lhj-bin/command/bin/model.rb', line 32

def gen_model
  model = fetch_model
  fetch_models(nil, model) if model
  print_models
  print_models_implementation
end

#handle_plistObject



23
24
25
26
27
28
29
30
# File 'lib/cocoapods-lhj-bin/command/bin/model.rb', line 23

def handle_plist
  entitlements_file = File.expand_path('~/config.plist')
  result = Plist.parse_xml(entitlements_file)
  result.delete('com.apple.security.application-groups')
  result['type'] = '0'
  result.delete('version')
  result.save_plist(entitlements_file)
end


63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cocoapods-lhj-bin/command/bin/model.rb', line 63

def print_models
  @models.each do |model|
    model_name = ''
    model_name = model[:name].gsub('List', '').gsub('Vo', '').gsub(/^\w/) { $&.upcase } if model[:name]
    puts "@interface #{@model_pre_name}#{model_name}Model : NSObject"
    model[:value].each do |key, value|
      print_property(key, value)
    end
    puts "@end\n\n\n"
  end
end


75
76
77
78
79
80
81
82
83
84
# File 'lib/cocoapods-lhj-bin/command/bin/model.rb', line 75

def print_models_implementation
  @models.each do |model|
    model_name = model[:name].gsub('List', '').gsub('Vo', '').gsub(/^\w/) { $&.upcase } if model[:name]
    puts "@implementation #{@model_pre_name}#{model_name}Model"
    puts "+(NSDictionary *)modelContainerPropertyGenericClass {"
    puts "  return @{@\"#{model[:name]}\" : #{@model_pre_name}#{model_name}Model.class};"
    puts "}"
    puts "@end\n\n\n"
  end
end


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cocoapods-lhj-bin/command/bin/model.rb', line 86

def print_property(key, value)
  if value.instance_of? String
    puts "///#{value}"
    puts "@property (nonatomic, copy) NSString *#{key};"
  elsif value.instance_of? Integer
    puts "///#{value}"
    puts "@property (nonatomic, assign) NSInteger #{key};"
    puts "///#{value}" if value > 1000
    puts "@property (nonatomic, strong) MLCentNumber *#{key};" if value > 1000
  elsif value.instance_of? Float
    puts "///#{value}"
    puts "@property (nonatomic, assign) CGFloat #{key};"
  elsif (value.instance_of? TrueClass) || (value.instance_of? FalseClass)
    puts "///#{value}"
    puts "@property (nonatomic, assign) BOOL #{key};"
  elsif value.instance_of? Array
    if value.first.instance_of? String
      puts "///#{key}"
      puts "@property (nonatomic, strong) NSArray<NSString *> *#{key};"
    else
      puts "///#{key}"
      name = key.gsub('List', '').gsub('Vo', '').gsub(/^\w/) { $&.upcase }
      puts "@property (nonatomic, strong) NSArray<#{@model_pre_name}#{name}Model *> *#{key};"
    end
  elsif value.instance_of? Hash
    puts "///#{key}"
    name = key.gsub('List', '').gsub('Vo', '').gsub(/^\w/) { $&.upcase }
    puts "@property (nonatomic, strong) #{@model_pre_name}#{name}Model *#{key};"
  else
    puts "///#{value}"
    puts "@property (nonatomic, copy) NSString *#{key};"
  end
end

#runObject



18
19
20
21
# File 'lib/cocoapods-lhj-bin/command/bin/model.rb', line 18

def run
  gen_model
  # handle_plist
end

#validate!Object



46
47
48
# File 'lib/cocoapods-lhj-bin/command/bin/model.rb', line 46

def validate!
  help! "请输入url" unless @url
end