jsoncop 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a59d17ecb175894205580ffe2427ac85c4eb052b
4
+ data.tar.gz: b720d5fc5bdcd7b51881eceec0392a0360a2eb0f
5
+ SHA512:
6
+ metadata.gz: 44e6fff44d7d366e14af38c2694b6472208de94896a6cad5291f1a7852f7f136f1557f0b760fec7b3955e387de188cf9c051477c9bf2591a0a629b5fbd40adee
7
+ data.tar.gz: a2386cf061216c2dd79f4103ead2490bb411138ac1cd1c52aafe9254d96cfeac6cf12a58b2680d065546417e1df591f696db86d7808d6ead63dc48efb09d9753
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Draveness
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # JSONCop
2
+
3
+ JSONCop makes it easy to write a simple model layer for your Cocoa and Cocoa Touch application.
4
+
5
+ > JSONCop's APIs are highly inspired by [Mantle](https://github.com/Mantle/Mantle)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'jsoncop'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install jsoncop
22
+
23
+ ## Usage
24
+
25
+
26
+ ## Contributing
27
+
28
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jsoncop. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
29
+
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/bin/cop ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'jsoncop'
4
+
5
+ JSONCop::Command.run(ARGV)
data/lib/jsoncop.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "jsoncop/version"
2
+
3
+ module JSONCop
4
+
5
+ class Informative < StandardError; end
6
+
7
+ require 'jsoncop/version'
8
+ require 'jsoncop/helper'
9
+
10
+ autoload :Command, 'jsoncop/command'
11
+ end
@@ -0,0 +1,44 @@
1
+ module JSONCop
2
+ class Analyzer
3
+
4
+ require 'jsoncop/model/model'
5
+ require 'jsoncop/model/attribute'
6
+
7
+ JSON_COP_ENABLED = /jsoncop: enabled/
8
+
9
+ MODEL_NAME_REGEX = /(struct|class)\s+(.+)\s*{/
10
+ ATTRIBUTE_REGEX = /^\s+(let|var)\s(.+):(.+)/
11
+ JSON_TRANSFORMER_REGEX = /^\s+static\s+func\s+(.+)JSONTransformer.+->.+/
12
+ JSON_BY_PROPERTY_HASH_REGEX = /static\s+func\s+JSONKeyPathByPropertyKey\(\)\s*->\s*\[String\s*:\s*String\]\s*{\s*return\s*(\[[\s"a-z0-9A-Z_\-:\[\],]*)}/
13
+
14
+ attr_reader :file_path
15
+ attr_accessor :model
16
+
17
+ def initialize(file_path)
18
+ @file_path = file_path
19
+ end
20
+
21
+ def analyze!
22
+ content = File.read file_path
23
+ return unless content =~ JSON_COP_ENABLED
24
+ content.each_line do |line|
25
+ if line =~ MODEL_NAME_REGEX
26
+ model_name = line.scan(MODEL_NAME_REGEX).flatten.last
27
+ @model = Model::Model.new model_name
28
+ elsif line =~ ATTRIBUTE_REGEX
29
+ result = line.scan(ATTRIBUTE_REGEX).flatten
30
+ @model.attributes << Model::Attribute.new(result[1], result[2])
31
+ elsif line =~ JSON_TRANSFORMER_REGEX
32
+ result = line.scan(JSON_TRANSFORMER_REGEX).flatten
33
+ @model.transformers << result.first
34
+ end
35
+ end
36
+
37
+ json_attr_list = content.scan(JSON_BY_PROPERTY_HASH_REGEX).flatten.first
38
+ json_attr_list.gsub!(/[(\[\]"\s)]*/, '')
39
+ @model.attr_json_hash = Hash[json_attr_list.split(",").map { |attr_json_pair| attr_json_pair.split(":").reverse }]
40
+ @model
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,29 @@
1
+ require 'colored'
2
+ require 'claide'
3
+
4
+ module JSONCop
5
+ class Command < CLAide::Command
6
+ require "jsoncop/command/install"
7
+ require "jsoncop/command/uninstall"
8
+
9
+ self.abstract_command = true
10
+ self.command = 'cop'
11
+ self.version = VERSION
12
+ self.description = 'JSONCop, the JSON parsing methods generator.'
13
+ self.plugin_prefixes = %w(claide meta)
14
+
15
+ def self.run(argv)
16
+ raise Informative, "JSONCop must run in project root folder which contains a xcodeproj file" \
17
+ unless Dir.glob("*.xcodeproj").count > 0
18
+ super(argv)
19
+ end
20
+
21
+ def self.options
22
+ super
23
+ end
24
+
25
+ def initialize(argv)
26
+ super
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,36 @@
1
+ module JSONCop
2
+ class Command
3
+ class Install < Command
4
+
5
+ require "jsoncop/analyzer"
6
+ require "jsoncop/generator"
7
+
8
+ self.summary = ""
9
+ self.description = <<-DESC
10
+ DESC
11
+
12
+ def initialize(argv)
13
+ super
14
+ end
15
+
16
+ def run
17
+ Dir.glob("**/*.swift").each do |file|
18
+ analyzer = create_analyzer_for_file file
19
+ model = analyzer.analyze!
20
+ next unless model
21
+
22
+ generator = create_generator_for_file file, model
23
+ generator.generate!
24
+ end
25
+ end
26
+
27
+ def create_analyzer_for_file(file_path)
28
+ Analyzer.new file_path
29
+ end
30
+
31
+ def create_generator_for_file(file_path, model)
32
+ Generator.new file_path, model
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,16 @@
1
+ module JSONCop
2
+ class Command
3
+ class Deintegrate < Command
4
+ self.summary = ""
5
+ self.description = <<-DESC
6
+ DESC
7
+
8
+ def initialize(argv)
9
+ super
10
+ end
11
+
12
+ def run
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,53 @@
1
+ module JSONCop
2
+ class Generator
3
+
4
+ require 'jsoncop/model/model'
5
+ require 'jsoncop/model/attribute'
6
+
7
+ attr_reader :file_path
8
+ attr_reader :model
9
+
10
+ def initialize(file_path, model)
11
+ @file_path = file_path
12
+ @model = model
13
+ end
14
+
15
+ def generate!
16
+ jsoncop_generated_start = /jsoncop: generated\-start/
17
+ jsoncop_generated_end = /jsoncop: generated\-end/
18
+ content = File.read file_path
19
+ if content.match(jsoncop_generated_start) && content.match(jsoncop_generated_end)
20
+ content.gsub!(/\/\/ jsoncop: generated-start[^$]*jsoncop: generated\-end/, "")
21
+ end
22
+ File.write file_path, content + json_cop_template
23
+ end
24
+
25
+ def json_cop_template
26
+ <<-JSONCOP
27
+ // jsoncop: generated-start
28
+
29
+ extension #{@model.name} {
30
+ static func parse(json: [String: Any]) -> #{@model.name}? {
31
+ guard #{json_parsing_template} else { return nil }
32
+ return #{@model.name}(#{model.key_value_pair})
33
+ }
34
+ static func parse(jsons: [[String: Any]]) -> [#{@model.name}] {
35
+ return jsons.flatMap(parse)
36
+ }
37
+ }
38
+
39
+ // jsoncop: generated-end
40
+ JSONCOP
41
+ end
42
+
43
+ def json_parsing_template
44
+ @model.attributes.map do |attr|
45
+ if @model.transformers.include? attr.name
46
+ "let #{attr.name} = (json[\"#{@model.attr_json_hash[attr.name]}\"]).flatMap(#{attr.name}JSONTransformer)"
47
+ else
48
+ "let #{attr.name} = json[\"#{@model.attr_json_hash[attr.name]}\"] as? #{attr.type}"
49
+ end
50
+ end.join(",\n\t\t")
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,9 @@
1
+ module JSONCop
2
+ module Helper
3
+ class String
4
+ def clear_white_space
5
+ self.gsub(/\s+/, "")
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ module JSONCop
2
+ module Model
3
+ class Attribute
4
+ attr_reader :name, :type
5
+ def initialize(name, type)
6
+ @name = name.gsub(/\s+/, "")
7
+ @type = type.gsub(/\s+/, "")
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ module JSONCop
2
+ module Model
3
+ class Model
4
+ attr_reader :name
5
+ attr_accessor :attributes
6
+ attr_accessor :transformers
7
+ attr_accessor :attr_json_hash
8
+
9
+ def initialize(name)
10
+ @name = name.gsub(/\s+/, "")
11
+ @attributes = []
12
+ @transformers = []
13
+ @attr_json_hash = {}
14
+ end
15
+
16
+ def key_value_pair
17
+ attributes.map { |attribute| "#{attribute.name}: #{attribute.name}" }.join(", ")
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module JSONCop
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsoncop
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Draveness
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: claide
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 1.0.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: colored
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.2'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.2'
47
+ - !ruby/object:Gem::Dependency
48
+ name: activesupport
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 4.2.6
54
+ - - "<"
55
+ - !ruby/object:Gem::Version
56
+ version: '5.0'
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 4.2.6
64
+ - - "<"
65
+ - !ruby/object:Gem::Version
66
+ version: '5.0'
67
+ - !ruby/object:Gem::Dependency
68
+ name: bundler
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '1.12'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '1.12'
81
+ - !ruby/object:Gem::Dependency
82
+ name: rake
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '10.0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '10.0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: rspec
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '3.0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: '3.0'
109
+ description: A light-weight JSON to model method generator.
110
+ email:
111
+ - stark.draven@gmail.com
112
+ executables:
113
+ - cop
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - LICENSE
118
+ - README.md
119
+ - bin/cop
120
+ - lib/jsoncop.rb
121
+ - lib/jsoncop/analyzer.rb
122
+ - lib/jsoncop/command.rb
123
+ - lib/jsoncop/command/install.rb
124
+ - lib/jsoncop/command/uninstall.rb
125
+ - lib/jsoncop/generator.rb
126
+ - lib/jsoncop/helper.rb
127
+ - lib/jsoncop/model/attribute.rb
128
+ - lib/jsoncop/model/model.rb
129
+ - lib/jsoncop/version.rb
130
+ homepage: https://github.com/Draveness/JSONCop
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.6.4
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: A JSON to model method generator.
154
+ test_files: []