jacopo beschi: yet another web guy

Metaprogramming Archive

Solve problems gracefully with dynamic method generation in ruby

Created at: Aug/20/2015 jacopo beschi

Days ago i was writing an Rspec macro to gracefully handle authenticated api via a token. At the start i begun creating a couple of methods, each for every Rest verb: module RequestMacros def get_authorized(uri, user) get uri, nil, {'X-Api-Token' => user.api_token} end

  def post_authorized(uri, data, user, headers = {})
    post uri, data, headers.merge({'X-Api-Token' => user.api_token})
Read on

Ruby: Dynamically create callbacks with metaprogramming

Created at: Jun/12/2015 jacopo beschi

Hello guys, in this post I'll explain you how you can handle callbacks with metaprogramming on ruby on rails. But before going deeper into detail you should ask me the reason of that: why shall you use callbacks instead of using general oop techniques? For example in a classic oop design given that you have:

Class X
   def method_x(*args)
      #do something
Read on

Testing for custom Rails validators with Rspec and metaprogramming

Created at: Apr/25/2015 jacopo beschi

Hello guys, some days ago I've made a custom validator for Rails and I wanted to test that it was used correctly in my model (which uses ActiveModel::Model). As a brief preface you have to know that to test for the common Rails validators you can use the shoulda matchers library.

Read on