jsoncop 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +52 -11
- data/lib/jsoncop/command/uninstall.rb +10 -1
- data/lib/jsoncop/generator.rb +6 -6
- data/lib/jsoncop/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe2dadd4db52fbf4f373d7219133ee6a400c3c61
|
4
|
+
data.tar.gz: e7ce11ffaa6a534e095b73d5301e6f04a622181d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae267be67d57306b869c5e5bc267691941d3a4ea25dfff9188d322646da8e6d1187cb8d8268a841442c5fe5d965e55cc407e23470219e15c8bac12528eebc290
|
7
|
+
data.tar.gz: d1bbf93c5b176cf40c734113727e09068c0b2e2ee0d16b582a59858add6554023af9941350c096c140d2579ba63914092b575a7a493455d66aef0be66969caf9
|
data/README.md
CHANGED
@@ -1,31 +1,72 @@
|
|
1
1
|
# JSONCop
|
2
2
|
|
3
|
+
[![License](https://img.shields.io/badge/license-MIT-green.svg?style=flat)](https://github.com/draveness/jsoncop/blob/master/LICENSE)
|
4
|
+
[![Gem](https://img.shields.io/gem/v/jsoncop.svg?style=flat)](http://rubygems.org/gems/jsoncop)
|
5
|
+
[![Swift](https://img.shields.io/badge/swift-3.0-yellow.svg)](https://img.shields.io/badge/Swift-%203.0%20-yellow.svg)
|
6
|
+
|
3
7
|
JSONCop makes it easy to write a simple model layer for your Cocoa and Cocoa Touch application.
|
4
8
|
|
5
|
-
> JSONCop's APIs are highly inspired by [Mantle](https://github.com/Mantle/Mantle)
|
9
|
+
> JSONCop's APIs are highly inspired by [Mantle](https://github.com/Mantle/Mantle), you can use similar APIs to generate parsing methods with JSONCop.
|
6
10
|
|
7
|
-
|
11
|
+
```swift
|
12
|
+
let json: [String: Any] = [
|
13
|
+
"id": 1,
|
14
|
+
"name": "Draven",
|
15
|
+
"createdAt": NSTimeIntervalSince1970
|
16
|
+
]
|
17
|
+
let person = Person.parse(json: json)
|
18
|
+
```
|
8
19
|
|
9
|
-
|
20
|
+
## Usage
|
10
21
|
|
11
|
-
|
12
|
-
|
22
|
+
Define a model with and implement `static func JSONKeyPathByPropertyKey` method:
|
23
|
+
|
24
|
+
```swift
|
25
|
+
// jsoncop: enabled
|
26
|
+
|
27
|
+
struct Person {
|
28
|
+
let id: Int
|
29
|
+
let name: String
|
30
|
+
let createdAt: Date
|
31
|
+
|
32
|
+
static func JSONKeyPathByPropertyKey() -> [String: String] {
|
33
|
+
return [
|
34
|
+
"id": "id",
|
35
|
+
"name": "name",
|
36
|
+
"createdAt": "createdAt"
|
37
|
+
]
|
38
|
+
}
|
39
|
+
|
40
|
+
static func createdAtJSONTransformer(value: Any) -> Date? {
|
41
|
+
guard let value = value as? Double else { return nil }
|
42
|
+
return Date(timeIntervalSinceNow: value)
|
43
|
+
}
|
44
|
+
}
|
13
45
|
```
|
14
46
|
|
15
|
-
|
47
|
+
> DO NOT forget to add "// jsoncop: enabled" before struct.
|
16
48
|
|
17
|
-
|
49
|
+
Run `cop install` in project root folder.
|
18
50
|
|
19
|
-
|
51
|
+
```shell
|
52
|
+
$ cop install
|
53
|
+
```
|
20
54
|
|
21
|
-
|
55
|
+
This will generate several parsing methods in current file:
|
22
56
|
|
23
|
-
|
57
|
+
![](./images/jsoncop-demo.png)
|
24
58
|
|
59
|
+
All the code between `generate-start` and `generate-end` and will be replaced when re-run `cop install` in current project folder. Other codes will remain unchanged. Please don't write any codes in this area.
|
60
|
+
|
61
|
+
## Installation
|
62
|
+
|
63
|
+
```
|
64
|
+
sudo gem install jsoncop --verbose
|
65
|
+
```
|
25
66
|
|
26
67
|
## Contributing
|
27
68
|
|
28
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
69
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/draveness/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
70
|
|
30
71
|
|
31
72
|
## License
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module JSONCop
|
2
2
|
class Command
|
3
|
-
class
|
3
|
+
class Uninstall < Command
|
4
4
|
self.summary = ""
|
5
5
|
self.description = <<-DESC
|
6
6
|
DESC
|
@@ -10,6 +10,15 @@ module JSONCop
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def run
|
13
|
+
Dir.glob("**/*.swift").each do |file_path|
|
14
|
+
jsoncop_generate_start = /jsoncop: generate\-start/
|
15
|
+
jsoncop_generate_end = /jsoncop: generate\-end/
|
16
|
+
content = File.read file_path
|
17
|
+
if content.match(jsoncop_generate_start) && content.match(jsoncop_generate_end)
|
18
|
+
content.gsub!(/\/\/ jsoncop: generate-start[^$]*jsoncop: generate\-end/, "")
|
19
|
+
end
|
20
|
+
File.write file_path, content
|
21
|
+
end
|
13
22
|
end
|
14
23
|
end
|
15
24
|
end
|
data/lib/jsoncop/generator.rb
CHANGED
@@ -13,18 +13,18 @@ module JSONCop
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def generate!
|
16
|
-
|
17
|
-
|
16
|
+
jsoncop_generate_start = /jsoncop: generate\-start/
|
17
|
+
jsoncop_generate_end = /jsoncop: generate\-end/
|
18
18
|
content = File.read file_path
|
19
|
-
if content.match(
|
20
|
-
content.gsub!(/\/\/ jsoncop:
|
19
|
+
if content.match(jsoncop_generate_start) && content.match(jsoncop_generate_end)
|
20
|
+
content.gsub!(/\/\/ jsoncop: generate-start[^$]*jsoncop: generate\-end/, "")
|
21
21
|
end
|
22
22
|
File.write file_path, content + json_cop_template
|
23
23
|
end
|
24
24
|
|
25
25
|
def json_cop_template
|
26
26
|
<<-JSONCOP
|
27
|
-
// jsoncop:
|
27
|
+
// jsoncop: generate-start
|
28
28
|
|
29
29
|
extension #{@model.name} {
|
30
30
|
static func parse(json: [String: Any]) -> #{@model.name}? {
|
@@ -36,7 +36,7 @@ extension #{@model.name} {
|
|
36
36
|
}
|
37
37
|
}
|
38
38
|
|
39
|
-
// jsoncop:
|
39
|
+
// jsoncop: generate-end
|
40
40
|
JSONCOP
|
41
41
|
end
|
42
42
|
|
data/lib/jsoncop/version.rb
CHANGED