Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to setup dependent properties? #35

Open
hexsprite opened this issue Jul 5, 2018 · 8 comments
Open

how to setup dependent properties? #35

hexsprite opened this issue Jul 5, 2018 · 8 comments

Comments

@hexsprite
Copy link

not sure if there's a way to do this or not, looking at the code so far I think not...

but I want to be able to say something like:

Factory.define('event', Actions, {
    end () {
      if (!this.start || !this.duration) {
        throw new Error('must provide start and duration if end not specified')
      }
      return new Date(+this.start + this.duration)
    },
})

That would allow me to optionally provide start and duration. I notice that this object contains the statically defined properties, but not what the user provides as arguments to Factory.create

Any tips on how to implement that?

@dburles
Copy link
Collaborator

dburles commented Jul 6, 2018

Hey @hexsprite just wondering what's your specific use case?

You should define your factories as being correct to begin with, i.e with all arguments required to build a valid document already present.

If you need to test with different start and duration values, I would either define a single event factory that generates random values or multiple factories with static dates. In the case of testing I would actually recommend being careful with random values since it can also randomly break tests.

@hexsprite
Copy link
Author

I would like to be able to provide a start and duration and if the end is not actually provided I want it to be calculated from those provided parameters. This would be a welcome shorthand for my tests as it means I don't have to do tedious calculations to present a correct end parameter....

@hexsprite
Copy link
Author

current:

    Factory.create('action', {
      userId,
      intendedDay,
      start: new Date('1977-03-21T23:30:00.000Z'),
      end: new Date('1977-03-22T00:30:00.000Z'),
      duration: 1 * HOURS,
      order: 1
    })

desired:

    Factory.create('action', {
      userId,
      intendedDay,
      start: new Date('1977-03-21T23:30:00.000Z'),
      duration: 1 * HOURS,
      order: 1
    })

@dburles
Copy link
Collaborator

dburles commented Jul 6, 2018

In your app does end live in the database or is it computed say through a collection-helper?

@hexsprite
Copy link
Author

needs to be in the DB

@hexsprite
Copy link
Author

though that is a good point since one of them should be computed ;) Not ready to make that sort of change in the app yet though

@dburles
Copy link
Collaborator

dburles commented Jul 6, 2018

It definitely shouldn't be the role of the factory to calculate the end field. You know what I mean? I assume you must have the same code elsewhere in the app like in an insert method?

A pattern I've done in the past is run the generated factory document through a prepare function (just as the method does with that same function during an insert)

@dburles
Copy link
Collaborator

dburles commented Jul 6, 2018

In fact I have a fork of the package that allows you to provide this very function as part of the factory definition ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants