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.
EasilyTypable alternatives and similar gems
Based on the "Abstraction" category.
Alternatively, view EasilyTypable alternatives based on common mentions on social networks and blogs.
-
Interactor
Interactor provides a common interface for performing complex user interactions. -
wisper
A micro library providing Ruby objects with Publish-Subscribe capabilities -
Responders
A set of Rails responders to dry up your application -
ActiveInteraction
:briefcase: Manage application specific business logic. -
Decent Exposure
A helper for creating declarative interfaces in controllers -
Mutations
Compose your business logic into commands that sanitize and validate input. -
Rails Event Store
A Ruby implementation of an Event Store based on Active Record -
dry-types
Flexible type system for Ruby with coercions and constraints -
Light Service
Series of Actions with an emphasis on simplicity. -
Amoeba
A ruby gem to allow the copying of ActiveRecord objects and their associated children, configurable with a DSL on the model -
SimpleCommand
A simple, standardized way to build and use Service Objects (aka Commands) in Ruby -
Waterfall
A slice of functional programming to chain ruby services and blocks, thus providing a new approach to flow control. Make them flow! -
u-service
Represent use cases in a simple and powerful way while writing modular, expressive and sequentially logical code. -
Surrounded
Create encapsulated systems of objects and focus on their interactions -
PageletRails
Improve perceived performance of your rails application with minimum effort -
Smart Init - Simple service objects in Ruby
A simple gem for eliminating Ruby initializers boilerplate code, and providing unified service objects API -
SuperModule
SuperModule allows defining class methods and method invocations the same way a super class does without using def included(base). This also succeeds ActiveSupport::Concern by offering lighter syntax -
Rocketman
๐ Rocketman help build event-based/pub-sub code in Ruby -
skinny_controllers
A pattern for allowing for easier testing of large projects' business logic -
Strategic
Strategic - Painless Strategy Pattern in Ruby and Rails -
Invokable
Objects are functions! Treat any Object or Class as a Proc (like Enumerable but for Procs). -
Lionshare
A Ruby interface to the Lionshare API (cryptocurrency prices) -
grpc_serializer
A simple library to encode nested hash to grpc object and vice versa -
simple_active_link_to
Simple rails view helper to manage "active" state of a link -
dry-rb
dry-rb is a collection of next-generation Ruby libraries, each intended to encapsulate a common task.
Clean code begins in your IDE with SonarLint
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of EasilyTypable or a related project?
README
EasilyTypable v1.0.2
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.