Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/geektree0101/vetty
Very easy commit & read & mutation mechanism about all of model, Reactive Model Provider built on RxSwift
https://github.com/geektree0101/vetty
ios model provider-engine reactive-programming rxswift rxswift-extensions
Last synced: about 1 month ago
JSON representation
Very easy commit & read & mutation mechanism about all of model, Reactive Model Provider built on RxSwift
- Host: GitHub
- URL: https://github.com/geektree0101/vetty
- Owner: GeekTree0101
- License: mit
- Created: 2018-12-01T04:40:25.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-12-15T03:02:43.000Z (about 6 years ago)
- Last Synced: 2024-10-25T16:42:41.851Z (about 2 months ago)
- Topics: ios, model, provider-engine, reactive-programming, rxswift, rxswift-extensions
- Language: Swift
- Homepage:
- Size: 281 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![alt text](https://github.com/GeekTree0101/Vetty/blob/master/res/logo.png)
[![CI Status](https://img.shields.io/travis/Geektree0101/Vetty.svg?style=flat)](https://travis-ci.org/Geektree0101/Vetty)
[![Version](https://img.shields.io/cocoapods/v/Vetty.svg?style=flat)](https://cocoapods.org/pods/Vetty)
[![License](https://img.shields.io/cocoapods/l/Vetty.svg?style=flat)](https://cocoapods.org/pods/Vetty)
[![Platform](https://img.shields.io/cocoapods/p/Vetty.svg?style=flat)](https://cocoapods.org/pods/Vetty)## Feature
Link: https://github.com/GeekTree0101/Vetty/projects/2
## Usage
### Make a model
```swift
class User: VettyProtocol { // <--- STEP1: Inherit VettyProtocol
var uniqueKey: VettyIdentifier {
return userId // <--- STEP2: return model uniqueKey
}
var userId: Int = -1
var username: String = ""
var profileURL: URL?
enum CodingKeys: String, CodingKey {
case userId = "id"
case username = "login"
case profileURL = "avatar_url"
}
func commitSubModelIfNeeds() { // <---- STEP3: (Optional)
// Will pass, user model doen't has VettyProtocol Sub-Model
}
}
```### Commit
1. Directly commit model
```swift
let userIdForVetty: VettyIdentifier = Vetty.shared.commit(user)
```2. Using Reactive Extension
```swift
let userIdForVettyObservable: Observable =
Observable.just(user)
.asObservable()
.map({ $0.map({ $0 as VettyProtocol }) })
.commits(ignoreSubModel: false)
```### READ
> Directly read model object from Vetty
```swift
let user: User? = Vetty.shared.read(type: User.self, uniqueKey: 12345)```
### Model Observable
```swift
let userObservable: Observable = Vetty.rx.observer(type: User.self, uniqueKey: 12345)
```### Mutating
1. Directly Mutating
```swift
guard let user = Vetty.shared.read(type: User.self, uniqueKey: 12345) else { return }
user.profileURL = URL(string: "https://avatars1.githubusercontent.com/u/19504988?s=460&v=4")
Vetty.shared.commit(user, ignoreSubModel: true)
```2. Using Observable Extension
```swift
let observable: Observable = Vetty.rx.observer(type: User.self, uniqueKey: 12345)
Observable.just(URL(string: "https://avatars1.githubusercontent.com/u/19504988?s=460&v=4"))
.mutate(with: observable,
{ user, newURL -> User? in
user?.profileURL = newURL
return user
})
.disposed(by: disposeBag)
```## Advanced
### Sub-Model Observable from Root-Model Observable
```swift
let repoObservable = Vetty.rx.observer(type: Repository.self, uniqueKey: repoId)
let userObservable = repoObservable
.filterNil()
.map { $0.user?.uniqueKey }
.asObserver(type: User.self)
```### Ignore Sub-Model Mutating
```swift
let observable: Observable = Vetty.rx.observer(type: User.self, uniqueKey: 12345)
Observable.just(URL(string: "https://avatars1.githubusercontent.com/u/19504988?s=460&v=4"))
.mutate(with: observable,
ignoreSubModel: true) <--- Default is true!
{ user, newURL -> User? in
user?.profileURL = newURL
return user
})
.disposed(by: disposeBag)
```### Non-Ignore Sub-Model with Latest Sub-Model
> Model Example, Repository has User(Sub-Model) property!
```swiftclass Repository: VettyProtocol {
var uniqueKey: VettyIdentifier {
return id
}
var id: Int = -1
var user: User?
var repositoryName: String?
var desc: String?
var isPrivate: Bool = false
var isForked: Bool = false
enum CodingKeys: String, CodingKey {
case id = "id"
case user = "owner"
case repositoryName = "full_name"
case desc = "description"
case isPrivate = "private"
case isForked = "fork"
}
func commitSubModelIfNeeds() {
Vetty.shared.commitIfNeeds(user)
}
}class User: VettyProtocol {
var uniqueKey: VettyIdentifier {
return userId
}
var userId: Int = -1
var username: String = ""
var profileURL: URL?
enum CodingKeys: String, CodingKey {
case userId = "id"
case username = "login"
case profileURL = "avatar_url"
}
func commitSubModelIfNeeds() {
}
}```
If you don't wanna update user model(sub-model) than you just should set ignoreSubModel as True.
But, If uou should update repository model(root-model) with latest user model from vetty
than you just follow under the example.
```swift
let observable: Observable = Vetty.rx.observer(type: Repository.self, uniqueKey: "repo-23")
Observable.just("New Repository Description")
.mutate(with: observable,
ignoreSubModel: false)
{ repo, newDesc -> Repository? in
if let userId = repo.user?.userId,
let latestUser = Vetty.shared.read(type: User.self, uniqueKey: userId) {
repo.user = latestUser
}
repo?.desc = newDesc
return repo
})
.disposed(by: disposeBag)
```
## Installation
Vetty is available through [CocoaPods](https://cocoapods.org). To install
it, simply add the following line to your Podfile:```ruby
pod 'Vetty'
```## Author
Geektree0101, [email protected]
## License
Vetty is available under the MIT license. See the LICENSE file for more info.