KrakenDev / PrediKit

An NSPredicate DSL for iOS, OSX, tvOS, & watchOS. Inspired by SnapKit and lovingly written in Swift.
MIT License
540 stars 28 forks source link

Builder needs method for querying object properties #4

Closed djbe closed 8 years ago

djbe commented 8 years ago

Case where we want to query an object with related objects, such as:

class Captain: NSObject {
    var name: String
}
class Ship: NSObject {
    var captain: Captain
}
NSPredicate(format: "captain == %@", someCaptain)
NSPredicate(format: "captain.name == 'Chief Supreme'")

There currently is no method to match an object property (example 1), or to match a related object's property (example 2).

Are there currently any plans for this?

hectormatos2011 commented 8 years ago

I completely forgot about this! I'll put one in tonight for you

hectormatos2011 commented 8 years ago

Any opinions on syntax? At the moment you can achieve this by dropping down into strings as well. includeIf.string("captain.name").isEqualTo("Chief Supreme")

hectormatos2011 commented 8 years ago

At the moment I'm thinking something along the lines of this:

NSPredicate(Ship.self) { includeIf in
    includeIf.memberObject(Captain.self).string("name").isEqualTo("Chief Supreme")
}
hectormatos2011 commented 8 years ago

@djbe

dariopellegrini commented 8 years ago

I think you’re sending messages to the wrong person.

Dario Pellegrini

http://www.dariopellegrini.com

Il giorno 27 mag 2016, alle ore 04:28, Hector Matos notifications@github.com ha scritto:

@djbe https://github.com/djbe — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/KrakenDev/PrediKit/issues/4#issuecomment-222045606, or mute the thread https://github.com/notifications/unsubscribe/AGJSY0XnG3TZqt19S7lrECRR8pgFCmCFks5qFlbcgaJpZM4IksGf.

hectormatos2011 commented 8 years ago

Oops! Sorry but the username for the OP is @djbe. Is that yours as well?

hectormatos2011 commented 8 years ago

GitHub is showing David Jennes for that username

dariopellegrini commented 8 years ago

I think it’s because I’ve set as favorite this library. Sorry, I didn’t remember that. However that’s not my username

Dario Pellegrini

http://www.dariopellegrini.com

Il giorno 27 mag 2016, alle ore 14:24, Hector Matos notifications@github.com ha scritto:

GitHub is showing David Jennes for that username

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/KrakenDev/PrediKit/issues/4#issuecomment-222133031, or mute the thread https://github.com/notifications/unsubscribe/AGJSY_m7amwKWS8VNm4Af2ZpUjGOUP6Fks5qFuJkgaJpZM4IksGf.

djbe commented 8 years ago

@dariopellegrini He was actually mentioning me, not you :-)

@hectormatos2011 Your syntax suggestion looks great, although I'd make a small change. I wouldn't work with the class type for properties, the property might have a different name (such as "captain1" or "reserveCaptain"). It might be better to just use strings for property names (until we get #selector syntax for getters):

includeIf.memberObject("captain").string("name").isEqualTo("Chief Supreme")
-->
NSPredicate(format: "captain.name == 'Chief Supreme'")

How would you handle the first predicate? You'd still need a call to test object equality, regardless of basic types. Something along the lines of:

includeIf.object("captain").isEqualTo(someCaptain)
-->
NSPredicate(format: "captain == %@", someCaptain)
hectormatos2011 commented 8 years ago

I think I have an idea for it. I'll submit a PR and how would feel about reviewing it?

hectormatos2011 commented 8 years ago

@djbe

hectormatos2011 commented 8 years ago

Alright so I just pushed a new version. It should now be available in PrediKit 2.0.3.

djbe commented 8 years ago

@hectormatos2011 Looks great! I'll try to integrate PrediKit in one of our more complex apps and see if I hit any issues.

hectormatos2011 commented 8 years ago

Sounds great! Feel free to continue creating issues! And let me know how it goes!