Implementing an adapter using method_missing is an opportunity to learn about Ruby's method lookup and the contracts that metaprogramming should not break. No prior experience required!
= Dredd::Rack::Runner.new => #<Dredd::Rack::Runner:0x007f9d9285f7c8 ...> irb:003> dredd.object_id # for example => 70157372423140 1. Create a new instance of Dredd::Rack::Runner 2. Send the object_id message to our runner object 3. Get the expected value!
@ingredients.map{ |ingredient| ingredient.name } end # Metaprogramming! Yay! define_method(:add_ingredient) do |ingredient| @ingredients << ingredient end end
runner ONCE... dredd = Dredd::Rack::Runner.new.level(:info).color!.sorted! # And run Dredd without worrying about configuring it or # restarting the application server? dredd.run $ dredd api-blueprint.apib http://localhost:3000 \ --level=info --color --sorted
= Dredd::Rack::Runner.new.level(:info).color!.sorted! dredd.run $ # Written manually to the command line $ dredd api-blueprint.apib http://localhost:3000 \ --level=info --color --sorted
any real HTTP transaction... --hookfiles, -f Specifies a pattern to match files... --language, -a Language of hookfiles. Possible options... --sandbox, -b Load and run non trusted hooks code... --server, -g Run API backend server command and kill... --server-wait Set delay time in seconds between running... --init, -i Run interactive configuration. ... --custom, -j Pass custom key-value configuration data... --names, -n Only list names of requests... --only, -x Run only specified transaction name. ... --reporter, -r Output additional report format. ... --output, -o Specifies output file when using... [...] --version Show version number. class Dredd::Rack::Runner def dry_run! @command_options << ‘--dry-run’ end # ... def names! @command_options << ‘--names’ end def only(arg) @command_options += [‘--only’, arg] end # ... # many many more... end
any real HTTP transaction... --hookfiles, -f Specifies a pattern to match files... --language, -a Language of hookfiles. Possible options... --sandbox, -b Load and run non trusted hooks code... --server, -g Run API backend server command and kill... --server-wait Set delay time in seconds between running... --init, -i Run interactive configuration. ... --custom, -j Pass custom key-value configuration data... --names, -n Only list names of requests... --only, -x Run only specified transaction name. ... --reporter, -r Output additional report format. ... --output, -o Specifies output file when using... [...] --version Show version number.
Metaprogramming is a collection of techniques. Some of them are simple. - Adapters’ are good candidates for automation - Metaprogramming should be reasonable - Abstraction is reasonable, can be a criterion
authors: The Trouble is... by JMFenner https:/ /www.redbubble.com/people/jmfenner/works/25293292-the-trouble-is Nighthawks Pastiche by sweetmans https:/ /www.redbubble.com/people/sweetmans/works/20602742-nighthawks-pastiche DJ by Ali Gulec https:/ /www.redbubble.com/people/aligulec/works/8367544-dj Nosferatu by JoJo Sesames https:/ /www.redbubble.com/people/jojoseames/works/16629789-nosferatu The optimist / / rose tinted glasses by lauragraves https:/ /www.redbubble.com/people/lauragraves/works/23478426-the-optimist-rose-tinted-glasses Lion by LOVATTO https:/ /www.redbubble.com/people/lovatto/works/22502675-lion [1] Only detail views are displayed in this presentation, find the originals on Redbubble!
You can read this presentation again on SpeakerDeck. Copyright (C) 2017 Gonzalo Bulnes Guilpain Except where otherwise stated, this presentation is published under the term of the Creative Commons Attribution-ShareAlike 4.0 International License (CC BY-SA). All logos and trademarks belong to their respective authors. Artworks displayed in this presentation belong to their authors, see the corresponding credits page for details. The code shown here is part of Dredd::Rack. Dredd::Rack is free software, see details at https:/ /github.com/gonzalo-bulnes/dredd-rack. Want to become a better programmer? Join the Recurse Center! More at https:/ /www.recurse.com/faq