Posts

  • Ruby: Dynamically create callbacks with metaprogramming

    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:

  • Testing for custom Rails validators with Rspec and metaprogramming

    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. But in my case what I’ve done is to unit test the validator (I won’t discuss about that) and then I’ve made a custom matcher to verify that the given classes uses the validator correctly. In order to test for the validator presence in the model I’ve leveraged Ruby metaprogramming. Here is the code where the custom validator is used:

  • Devise remote authentication with rails 4.2

    Hello guys, I’ve been trying to make remote authentication working with devise and i found this useful post: devise remote authentication. The problem is that the post example wasn’t working correctly with new devise versions (3.4.x); In this post I’ll explain you the changes that you need to do to make it work with devise 3.4. Note: Before reading the following part read the other article. The problem are the changes to Devise Authenticatable class regarding the serialization methods. In order to make it work you need to use the following code in your RemoteAuthenticatable Strategy:

  • Coffeescript Model class with attr_accessor rails alike

    Nowadays I’ve been toying around with the fabolous coffeescript language and I’ve created a basic model class.
    This class allow you to use attr_accessor method as you can do in Ruby. If you don’t know what attr_accessor is you can take a look here: Ruby attr_accessor.
    Briefly what attr_accessor does is to let you access object attributes in “object.attribute” form (which in javascript is built in) but more importantly let you add a constrain on read/write permission of that attribute! In fact if you want the attribute to be readonly you need to use the “attr_reader” method, in the opposite case you need to use the “attr_writer” method. If you don’t want to add any constraint you can use the “attr_accessor”.

  • Strong Typing vs Duck Typing PHP, C#, Ruby

    If you have heard of strong and weak typing but doesn’t know the main difference and advantages/disadvantages of both the paradigms in this article I’ll try to help you having a better undestanding of the overall concept. The first thing you need to understand is what types are. When you save some data in the memory you can associate to that space of memory a data-type, by associating a type you can have different behavior when manipulating that piece of data. In an object orientet language generally there are two main categories of data:

subscribe via RSS