little-boxes icon indicating copy to clipboard operation
little-boxes copied to clipboard

Possible alternative DSL

Open kigster opened this issue 9 years ago • 1 comments

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 :)

kigster avatar Mar 30 '17 18:03 kigster

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.

manuelmorales avatar Mar 31 '17 09:03 manuelmorales