Latest 0.3.0
Homepage https://github.com/vknabel/ValidatedInjectAdditions
License MIT
Platforms ios 8.0, osx 10.10, tvos 9.0, watchos 2.0, requires ARC
Dependencies EasyInject, ValidatedExtension
Authors

ValidatedInjectAdditions adds some convenience methods for projects that are using EasyInject and ValidatedExtension.

Installation

EasyInject is a Swift only project and supports Swift Package Manager, Carthage and CocoaPods.

Swift Package Manager

import PackageDescription

let package = Package(
    name: "YourPackage",
    dependencies: [
        .Package(url: "https://github.com/vknabel/EasyInject.git", majorVersion: 0, minor: 6),
        .Package(url: "https://github.com/vknabel/ValidatedExtension.git", majorVersion: 3, minor: 0),
        .Package(url: "https://github.com/vknabel/ValidatedInjectAdditions.git", majorVersion: 0, minor: 2)
    ]
)

Carthage

github "vknabel/EasyInject"
github "vknabel/ValidatedExtension"
github "vknabel/ValidatedInjectAdditions" ~> 0.2

CocoaPods

source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!

pod 'EasyInject'
pod 'ValidatedExtension'
pod 'ValidatedInjectAdditions', '~> 0.2'

Example

This code snippet is built on top of (EasyInject)[https://www.github.com/vknabel/EasyInject/README.md]’s example.

import EasyInject
import ValidatedExtension
import ValidatedInjectAdditions

// Define your a Validator as known from ValidatedExtension
struct NetworkServiceInjectorValidator: Validator {
    typealias WrappedType = AnyInjector<String>
    static func validate(_ value: AnyInjector<String>) throws -> Bool {
        let base = try value.resolving(from: .baseUrl)
        return URL(string: base)
            .flatMap({ _ in true })
            ?? false
    }
}

// Add properties to Validated<NetworkServiceInjectorValidator>
extension ValidatedType where ValidatorType == NetworkServiceInjectorValidator {
    var baseUrl: String {
        return try! value.resolving(from: .baseUrl)
    }
}

final class NetworkService: Providable, ValidatedDependency {
    // Declare your dependency
    typealias DependencyValidator = NetworkServiceInjectorValidator

    let baseUrl: String
    init<VT : ValidatedType where VT.ValidatorType == DependencyValidator>(validated dependencies: VT) {
        // Just focus on setting thing up 
        baseUrl = dependencies.baseUrl
    }
}

var injector = LazyInjector<String>().globalize().erase()
injector.provide(for: .baseUrl, usingFactory: { _ in "https://my.base.url/" })

// this will throw an error if .baseUrl is missing
// Otherwise initializes NetworkService
let networkService = try NetworkService(injector: &injector)

Author

Valentin Knabel, [email protected]

License

ValidatedInjectAdditions is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "ValidatedInjectAdditions",
    "version": "0.3.0",
    "summary": "Adds convenience when using ValidatedExtension and EasyInject",
    "description": "ValidatedExtension-EasyInject is a u03bc-library that adds convenience methods to EasyInject and ValidatedExtension.",
    "homepage": "https://github.com/vknabel/ValidatedInjectAdditions",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Valentin Knabel": "[email protected]"
    },
    "social_media_url": "https://twitter.com/vknabel",
    "source": {
        "git": "https://github.com/vknabel/ValidatedInjectAdditions.git",
        "tag": "0.3.0"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "tvos": "9.0",
        "watchos": "2.0"
    },
    "requires_arc": true,
    "source_files": "Sources/**/*.swift",
    "dependencies": {
        "EasyInject": [
            "~> 0.8"
        ],
        "ValidatedExtension": [
            "~> 4.0"
        ]
    }
}

Pin It on Pinterest

Share This