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

Possible alternative DSL #3

Open
kigster opened this issue Mar 30, 2017 · 1 comment
Open

Possible alternative DSL #3

kigster opened this issue Mar 30, 2017 · 1 comment

Comments

@kigster
Copy link

kigster commented Mar 30, 2017

I really like the idea of lazy evaluations and initialization.

But I am not sure that the vocabulary borrowed from RSpec ("let") sits with me well.

What if you could just use the words lazy and eager instead of let and eager?

And letc I would propose to merge with let (and replace by eager/lazy), and instead of the current dependency use depends or inject. This would better express that lazy/eager is for defining a dependency that will be used elsewhere, while depends or inject is for expressing a dependency on the externally defined object.

Also, instead of receiving box as an argument, perhaps the lambda can be run as instance_exec from the box itself, so that you don't need to provide box. and can just type this:

class MyApp::MainBox
  include LittleBoxes::Box

  lazy(:redis) do
    require 'redis'
    Redis.new
  end

  eager(:publisher) do # no parameter, because evaluated inside of box.instance_exec
    Publisher.new redis: redis
  end
end

class Publisher
  include LittleBoxes::Configurable
  inject :redis
end

class MainBox
  # ...

  lazy(:publisher) { Publisher.new }
end

What is the purpose of letc and why not just always resolve dependencies? I totally get the need for lazy and eager, but I wasn't really clear why you'd need two methods to define objects in the mainbox.

Overall — interesting concept and definitely worth looking into. Thanks for your work, and feel free to throw away all the suggestions. After all, naming is highly subjective if not personal :)

@manuelmorales
Copy link
Owner

Hi @kigster, thank you very much for your suggestions. They are very interesting. Let me reply to them one by one:

What if you could just use the words lazy and eager instead of let and eager?
...and instead of the current dependency use depends or inject

Sounds like a good alternative. I wouldn't consider making this change at this stage though.

And letc I would propose to merge with let

I actually thought about this when I started the library. I thought on relying on responds_to? to check if the object is configurable. However I learned that relying on responds_to? can be a can of worms and leads to unexpected behavior. I decided to discard it.

Also, instead of receiving box as an argument, perhaps the lambda can be run as instance_exec from the box itself, so that you don't need to provide box.

I also thought about this. instance_exec can bring unexpected behavior too, like preventing access to the outside scope.

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