Giter Site home page Giter Site logo

iron-ham / tailor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zenangst/tailor

0.0 1.0 0.0 621 KB

:necktie:A super fast & convenient object mapper tailored for your needs

Home Page: https://github.com/zenangst

License: Other

Swift 97.57% Ruby 2.43%

tailor's Introduction

Tailor Swift logo

CI Status Version Carthage Compatible License Platform Documentation Swift

A super fast & convenient object mapper tailored for your needs.

Mapping objects to arrays or dictionaries can be a really cumbersome task, but those days are over. Tailor features a whole bunch of nifty methods for your model sewing needs.

Mapping properties

Tailor features property, relation(s) mapping for both struct and class objects.

Struct

struct Person: Mappable {

  var firstName: String? = ""
  var lastName: String? = ""

  init(_ map: [String : Any]) {
    firstName <- map.property("first_name")
    lastName  <- map.property("last_name")
  }
}

let dictionary = ["first_name" : "Taylor", "last_name" : "Swift"]
let model = Person(dictionary)

Class

class Person: Mappable {

  var firstName: String? = ""
  var lastName: String? = ""

  required convenience init(_ map: [String : AnyObject]) {
    self.init()
    firstName <- map.property("first_name")
    lastName  <- map.property("last_name")
  }
}

let dictionary = ["first_name" : "Taylor", "last_name" : "Swift"]
let model = Person(dictionary)

Object mapping

struct Person: Mappable {

  var firstName: String? = ""
  var lastName: String? = ""
  var spouse: Person?

  init(_ map: [String : Any]) {
    firstName <- map.property("first_name")
    lastName  <- map.property("last_name")
    spouse    <- map.relation("spouse")
  }
}

let dictionary = [
  "first_name" : "Taylor",
  "last_name" : "Swift",
  "spouse" : ["first_name" : "Calvin",
              "last_name" : "Harris"]
]
let model = Person(dictionary)

Mapping objects

struct Person: Mappable {

  var firstName: String? = ""
  var lastName: String? = ""
  var spouse: Person?
  var parents = [Person]()

  init(_ map: [String : Any]) {
    firstName <- map.property("first_name")
    lastName  <- map.property("last_name")
    spouse    <- map.relation("spouse")
    parents   <- map.relations("parents")
  }
}

let dictionary = [
  "first_name" : "Taylor",
  "last_name" : "Swift",
  "spouse" : ["first_name" : "Calvin",
              "last_name" : "Harris"],
  "parents" : [
             ["first_name" : "Andrea",
              "last_name" : "Swift"],
              ["first_name" : "Scott",
              "last_name" : "Swift"]
  ]
]
let model = Person(dictionary)

SafeMappable

struct ImmutablePerson: SafeMappable {
  let firstName: String
  let lastName: String
  let spouse: Person
  let parents = [Person]()

  init(_ map: [String : Any]) throws {
    firstName = try <-map.property("firstName")
    lastName = try <-map.property("lastName")
    spouse = try <-map.relationOrThrow("spouse")
    parents = try <-map.relationsOrThrow("parents")
  }
}

let immutablePerson: ImmutablePerson
do {
  immutablePerson = try TestImmutable(["firstName" : "foo" , "lastName" : "bar"])
} catch {
  print(error)
}

Transforms

struct Person: Mappable {

  var firstName: String? = ""
  var lastName: String? = ""
  var spouse: Person?
  var parents = [Person]()
  var birthDate = NSDate?

  init(_ map: [String : Any]) {
    firstName <- map.property("first_name")
    lastName  <- map.property("last_name")
    spouse    <- map.relation("spouse")
    parents   <- map.relations("parents")
    birthDate <- map.transform("birth_date", transformer: { (value: String) -> NSDate? in
      let dateFormatter = NSDateFormatter()
      dateFormatter.dateFormat = "yyyy-MM-dd"
      return dateFormatter.dateFromString(value)
    })
  }
}

let dictionary = [
  "first_name" : "Taylor",
  "last_name" : "Swift",
  "spouse" : ["first_name" : "Calvin",
              "last_name" : "Harris"],
  "parents" : [
             ["first_name" : "Andrea",
              "last_name" : "Swift"],
              ["first_name" : "Scott",
              "last_name" : "Swift"]
  ],
  "birth_date": "1989-12-13"
]
let model = Person(dictionary)

Installation

Tailor is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Tailor'

Contribute

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create pull request

Who made this?

tailor's People

Contributors

zenangst avatar vadymmarkov avatar onmyway133 avatar ramongilabert avatar

Watchers

Hesham Salman avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.