The command pattern (ish) in Ruby, using a DSL
MIT License
Enjoy the enforcing-DSL of this command-patterny gem.
** Should you use this?
No, you shouldn't. You should write code like this:
#+BEGIN_SRC ruby class MyClassThatDoesSomething def initialize(foo:, bar:) @foo = foo @bar = bar end
def call # do stuff end end #+END_SRC
This code is plain Ruby, everyone can understand it, it doesn't depend on anything. It's way better. You do not need this gem. YOU. DO. NOT. NEED. THIS. GEM.
** Current version
0.5.0
** Installation
Add this line to your application's Gemfile:
#+BEGIN_SRC ruby gem 'insane_hook' #+END_SRC
And then execute:
#+BEGIN_SRC bash $ bundle #+END_SRC
Or install it yourself as:
#+BEGIN_SRC bash $ gem install insane_hook #+END_SRC
** Usage
#+BEGIN_SRC ruby class YeOldeTaske < InsaneHook required :arg1, :arg2 required :arg0 optional :arg3, arg4: 3 optional :arg9 call do # Do some work here # If you really need to return something, even though you shouldn't, leak it: leak "meaningful value" end end #+END_SRC
Is equivalent to:
#+BEGIN_SRC ruby class YeOldeTaske
def self.call(**args)
new(**args).call
end
attr_reader :arg0, :arg1, :arg2, :arg3, :arg4, :arg9
def initialize(arg0:, arg1:, arg2:, arg3: nil, arg9: nil, arg4: 3)
@arg0 = arg0
@arg1 = arg1
@arg2 = arg2
@arg3 = arg3
@arg4 = arg4
@arg9 = arg9
end
def call
leak "meaningful value"
self
end
def leak(arg)
@_result = arg
end
def result
if instance_variable_defined?(:@_result)
@_result
else
raise InsaneHook::CommandNotRunError
end
end
end #+END_SRC
** Design decisions
call
is idiomatic Ruby. Procs and method objects respond to call
, so we are extending an existing Ruby pattern..new
and the #initialize
methods, meaning the object does not truly belong to the person writing the code: the programmer is limited by the provided framework.** Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
** Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/trevoke/insane_hook.
** License
The gem is available as open source under the terms of the MIT License.