Popularity
0.6
Growing
Activity
0.0
Stable
1
4
1

Description

Although polymorphism is a recommended standard in Object-Oriented programming for invoking varied behavior in an inheritance hierarchy, sometimes it is still useful to verify if a particular model belongs to a certain type when the behavior concerned does not belong to the model and is too small to require a Design Pattern like Strategy.

A common example in Rails is checking user roles before rendering certain parts of the view.

To avoid the model.is_a?(Admin) syntax, a more readable approach that developers resort to is to add an English-like DSL method that hides the details of Object-Oriented type checking: model.admin?

Implementing such methods manually gets repetitive and error-prone, so an easier way to get these methods automatically is to mixin the EasilyTypable module.

Monthly Downloads: 54
Programming language: Ruby
License: MIT License
Tags: Abstraction     Rails     Ruby     Ruby Library    
Latest version: v1.0.2

EasilyTypable alternatives and similar gems

Based on the "Abstraction" category.
Alternatively, view EasilyTypable alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of EasilyTypable or a related project?

Add another 'Abstraction' Gem

README

EasilyTypable v1.0.2

Gem Version Build Status Coverage Status Code Climate

Introduction:

Although polymorphism is a recommended standard in Object-Oriented programming for invoking varied behavior in an inheritance hierarchy, sometimes it is still useful to verify if a particular model belongs to a certain type when the behavior concerned does not belong to the model and is too small to require a Design Pattern like Strategy.

A common example in Rails is checking user roles before rendering certain parts of the view:

<% if user.is_a?(Admin) %>
  <%= link_to 'Admin', admin_dashboard_path %>
<% end %>
<% if user.is_a?(Customer) %>
  <%= link_to 'Customer Profile', customer_profile_path %>
<% end %>

To avoid the model.is_a?(Admin) syntax, a more readable approach that developers resort to is to add an English-like DSL method that hides the details of Object-Oriented type checking: model.admin?.

The Rails example above would then become:

<% if user.admin? %>
  <%= link_to 'Admin', admin_dashboard_path %>
<% end %>
<% if user.customer? %>
  <%= link_to 'Customer Profile', customer_profile_path %>
<% end %>

Implementing such methods manually gets repetitive and error-prone, so an easier way to get these methods automatically is to mixin the EasilyTypable module.

Example:

require 'easily_typable' # in IRB at cloned project directory, call this instead: require './lib/easily_typable' 

class Vehicle
  include EasilyTypable
end

class Car < Vehicle
end

class Truck < Vehicle
end

class Van < Vehicle
end


puts Car.new.vehicle? # prints true
puts Car.new.car? # prints true
puts Car.new.truck? # prints false
puts Car.new.van? # prints false

puts Truck.new.vehicle? # prints true
puts Truck.new.car? # prints false
puts Truck.new.truck? # prints true
puts Truck.new.van? # prints false

puts Van.new.vehicle? # prints true
puts Van.new.car? # prints false
puts Van.new.truck? # prints false
puts Van.new.van? # prints true

Release Notes

  • v1.0.2: Support namespaced subclasses
  • v1.0.1: Rails model lazy loading now loads EasilyTypable methods automagically
  • v1.0.0: Initial EasilyTypable implementation for Ruby

Contributing to EasilyTypable

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
  • Fork the project.
  • Start a feature/bugfix branch.
  • Commit and push until you are happy with your contribution.
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

  • Copyright (c) 2009-2020 Andy Maleh
  • See LICENSE.txt for further details.


*Note that all licence references and agreements mentioned in the EasilyTypable README section above are relevant to that project's source code only.