Skip to content

Commit

Permalink
Update extension naming
Browse files Browse the repository at this point in the history
  • Loading branch information
OrkhanAlikhanov committed May 29, 2021
1 parent c398ab4 commit 8f5edcd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
## Why?
You can do this:
```swift
let titleLabel = UILabel().ch
let titleLabel = UILabel().set
.text("Welcome")
.font(.systemFont(ofSize: 20))
.textAlignment(.center)
.ch
.get
```
Instead of this:
```swift
Expand Down Expand Up @@ -49,7 +49,7 @@ You will be able to do:
```swift
let myClass = MyClass()

myClass.ch
myClass.set
.text("It works")
.int(99)
.myEnum(.value2)
Expand All @@ -59,11 +59,11 @@ print(myClass.int) // 99
print(myClass.myEnum) // .value2

/// immediate usage
let myClass = MyClass().ch
let myClass = MyClass().set
.text("It works")
.int(99)
.myEnum(.value2)
.ch // gains the instance back
.get // gains the instance back
```

## How it works?
Expand Down
16 changes: 6 additions & 10 deletions Sources/ChainSwift/ChainSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

@dynamicMemberLookup
public struct Chain<Base> {
public var ch: Base
public var get: Base

init(_ base: Base) {
self.ch = base
self.get = base
}

public subscript<T>(dynamicMember keyPath: WritableKeyPath<Base, T>) -> Callable<Base, T> {
Callable(ch, keyPath: keyPath)
Callable(get, keyPath: keyPath)
}
}

Expand All @@ -37,16 +37,12 @@ public class Callable<Base, T> {
public protocol Chainable {
associatedtype ChainableBase

static var ch: Chain<ChainableBase>.Type { get }
var ch: Chain<ChainableBase> { get }
var set: Chain<ChainableBase> { get }
}

public extension Chainable {
static var ch: Chain<Self>.Type {
Chain<Self>.self
}

var ch: Chain<Self> {
var set: Chain<Self> {
Chain(self)
}
}

12 changes: 7 additions & 5 deletions Tests/ChainSwiftTests/ChainSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class MyClass {
var text = ""
var int = 0
var myEnum: MyEnum = .value1

static var next = 2
}

struct MyStruct {
Expand All @@ -34,7 +36,7 @@ final class ChainSwiftTests: XCTestCase {
XCTAssertEqual(myClass.int, 0)
XCTAssertEqual(myClass.myEnum, .value1)

myClass.ch
myClass.set
.text("It works")
.int(99)
.myEnum(.value2)
Expand All @@ -45,11 +47,11 @@ final class ChainSwiftTests: XCTestCase {
}

func testTakingChValueWorks() {
let myClass = MyClass().ch
let myClass = MyClass().set
.text("It works")
.int(99)
.myEnum(.value2)
.ch
.get

XCTAssertEqual(myClass.text, "It works")
XCTAssertEqual(myClass.int, 99)
Expand All @@ -63,11 +65,11 @@ final class ChainSwiftTests: XCTestCase {
XCTAssertEqual(myStruct.int, 0)
XCTAssertEqual(myStruct.myEnum, .value1)

let updatedMyStruct = myStruct.ch
let updatedMyStruct = myStruct.set
.text("It works")
.int(99)
.myEnum(.value2)
.ch
.get

XCTAssertEqual(myStruct.text, "")
XCTAssertEqual(myStruct.int, 0)
Expand Down

0 comments on commit 8f5edcd

Please sign in to comment.