https://github.com/andyobtiva/easily_typable
Ruby module that facilitates English-like type checking in an inheritance hierarchy via "type_name?" methods
https://github.com/andyobtiva/easily_typable
mixin rails ruby
Last synced: 4 months ago
JSON representation
Ruby module that facilitates English-like type checking in an inheritance hierarchy via "type_name?" methods
- Host: GitHub
- URL: https://github.com/andyobtiva/easily_typable
- Owner: AndyObtiva
- License: mit
- Created: 2009-01-15T02:32:14.000Z (over 17 years ago)
- Default Branch: master
- Last Pushed: 2023-04-12T05:19:46.000Z (about 3 years ago)
- Last Synced: 2025-06-22T18:52:50.852Z (about 1 year ago)
- Topics: mixin, rails, ruby
- Language: Ruby
- Homepage:
- Size: 48.8 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# EasilyTypable v1.0.2
[](http://badge.fury.io/rb/easily_typable)
[](https://travis-ci.org/AndyObtiva/easily_typable)
[](https://coveralls.io/github/AndyObtiva/easily_typable?branch=master)
[](https://codeclimate.com/github/AndyObtiva/easily_typable)
## 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:
```erb
<% 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:
```erb
<% 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:
```ruby
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.