Class: JSONCop::Generator
- Inherits:
-
Object
- Object
- JSONCop::Generator
- Defined in:
- lib/jsoncop/generator.rb
Instance Attribute Summary collapse
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #decode_template(model) ⇒ Object
- #generate! ⇒ Object
-
#initialize(file_path, models) ⇒ Generator
constructor
A new instance of Generator.
- #json_cop_template ⇒ Object
- #json_parsing_template(model) ⇒ Object
Constructor Details
#initialize(file_path, models) ⇒ Generator
Returns a new instance of Generator.
10 11 12 13 |
# File 'lib/jsoncop/generator.rb', line 10 def initialize(file_path, models) @file_path = file_path @models = models end |
Instance Attribute Details
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
7 8 9 |
# File 'lib/jsoncop/generator.rb', line 7 def file_path @file_path end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
8 9 10 |
# File 'lib/jsoncop/generator.rb', line 8 def model @model end |
Instance Method Details
#decode_template(model) ⇒ Object
89 90 91 92 93 |
# File 'lib/jsoncop/generator.rb', line 89 def decode_template(model) model.attributes.map do |attr| "let #{attr.name} = decoder.decode#{attr.decode_type}(forKey: \"#{attr.name}\") as? #{attr.type}" end.join(",\n\t\t\t") end |
#generate! ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/jsoncop/generator.rb', line 15 def generate! jsoncop_generate_start = /\/\/ MARK: \- JSONCop\-Start/ jsoncop_generate_end = /\/\/ JSONCop\-End/ content = File.read file_path if content.match(jsoncop_generate_start) && content.match(jsoncop_generate_end) content.gsub!(/\/\/ MARK: \- JSONCop\-Start[^$]*JSONCop\-End/, json_cop_template) else content += json_cop_template end File.write file_path, content end |
#json_cop_template ⇒ Object
27 28 29 30 31 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 58 59 60 61 |
# File 'lib/jsoncop/generator.rb', line 27 def json_cop_template result = "// MARK: - JSONCop-Start\n\n" @models.each do |model| result += <<-JSONCOP extension #{model.name} { static func parse(json: Any) -> #{model.name}? { guard let json = json as? [String: Any] else { return nil } guard #{json_parsing_template model} else { return nil } return #{model.name}(#{model.key_value_pair}) } static func parses(jsons: Any) -> [#{model.name}] { guard let jsons = jsons as? [[String: Any]] else { return [] } return jsons.flatMap(parse) } } JSONCOP # result += <<-CODING # extension #{model.name}: NSCoding { # func encode(with aCoder: NSCoder) { # # } # # init?(coder decoder: NSCoder) { # guard #{decode_template model} else { return nil } # # self.init(#{model.key_value_pair}) # } # } # # CODING end result += "// JSONCop-End" result end |
#json_parsing_template(model) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/jsoncop/generator.rb', line 63 def json_parsing_template(model) model.attributes.map do |attr| transformer = model.transformers.select { |t| t.name == attr.name }.first attr_key_path = model.attr_json_hash[attr.name] || attr.name return unless attr_key_path value = "json" key_paths = attr_key_path.split(".") key_paths.each_with_index do |key, index| value.insert 0, "(" value += "[\"#{key}\"] as? " if index == key_paths.count - 1 if transformer value += "#{transformer.type})" value += ".flatMap(#{attr.name}JSONTransformer)" else value += "#{attr.type})" end else value += "[String: Any])?" end end "let #{attr.name} = #{value}" end.join(",\n\t\t\t") end |