Wednesday, February 08, 2017

Easily Typable Gemified and Updated to Support Rails

Easily Typable:

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 %>
To avoid the model.is_a?(CertainType) syntax, a more readable approach that developers resort to is to add an English-like method that hides the details of type checking model.certain_type?.
The Rails example above would then become:
<% if user.admin? %>
  <%= link_to 'Admin', admin_dashboard_path %>
<% end %>
Implementing such methods manually gets repetitive after a while, so an easier way to get these methods automatically is to mixin the EasilyTypable module.
This was originally released back in 2009 when I worked at Obtiva, but not in Gem form, so I've gemified it and updated to support Rails lazy-loading of Models.

Example:

require 'easily_typable'

class TypeA
  include EasilyTypable
end

class TypeB < TypeA
end

class TypeC < TypeB
end

## RSpec of Easily Typable
require 'spec_helper'

describe EasilyTypable do
  it "adds type_a? method to TypeA object" do
    expect(TypeA.new.type_a?).to be_truthy
  end
  it "adds type_b? method to TypeA object" do
    expect(TypeA.new.type_b?).to be_falsey
  end
  it "adds type_c? method to TypeA object" do
    expect(TypeA.new.type_c?).to be_falsey
  end
  it "adds type_a? method to TypeB object" do
    expect(TypeB.new.type_a?).to be_truthy
  end
  it "adds type_b? method to TypeB object" do
    expect(TypeB.new.type_b?).to be_truthy
  end
  it "adds type_c? method to TypeB object" do
    expect(TypeB.new.type_c?).to be_falsey
  end
  it "adds type_a? method to TypeC object" do
    expect(TypeC.new.type_a?).to be_truthy
  end
  it "adds type_b? method to TypeC object" do
    expect(TypeC.new.type_b?).to be_truthy
  end
  it "adds type_c? method to TypeC object" do
    expect(TypeC.new.type_c?).to be_truthy
  end
end

Tuesday, May 03, 2016

SuperModule 2 Beta (v1.2.0)

SuperModule 2 has long been overdue. It was almost out a year ago, and then my MacBook Air got stolen with the near-finished code at a Second Cup cafe in Montreal (won't mention which one, but let's just say don't leave your laptop unattended in Downtown Montreal). Fortunately, I got parts of the code stored in a remote Git branch (yeah Git for the rescue!), so I just released SuperModule 2 as beta today to use on my employer's client project.

Feature Summary:
  • New super_module(name) syntax
  • Much simpler implementation with guaranteed correctness and no performance hit
  • Less memory footprint by not requiring method_source Ruby gem for v2 syntax
  • Backwards compatibility with v1 syntax
No need to remember to include SuperModule anymore with the use of the v2 super_module method. Much simpler usage as a result as per popular demand.

Since this version is beta, I'm very curious about user feedback. Let me know how it goes for you.

Enjoy Ruby Modules again!!! Write as simply as any superclass with all of the power of multiple mixins!!!

Cheers... Andy

Sunday, March 20, 2016

Cycle.js Model View Intent in Conference Room Booker

Conference Room Booker has been refactored to the Cycle.js recommended Model View Intent pattern (as shown in this code structure screenshot):


In other words, it has been simplified to the bare essence of a pure decomposable reactive JavaScript function staring with sources of data and ending with sinks.

Intents: represent user action sources, such as keyboard presses and mouse clicks.

Intent Example (bookingDurationSource.es6):


The intent above captures keyboard input for the duration of booking, and outputs duration/time hash.

Models: aggregate data into meaningful structures.

Model Example (bookedRoomTimingSource.es6):


This model aggregates elapsing time, booked room data, and entered duration into a bookedRoomTiming hash structure, which later gets used in rendering the view.

Views: render data into HTML using Virtual-DOM-based Hypersrcipt terse syntax.

View Example (bookingView.es6):


This view renders the top part of the screen shown below.

Conference Room Booker Example Screenshot (Entering Duration):




Conference Room Booker Example Screenshot (Booking Timer Started by Clicking Room):



Conference Room Booker Example Screenshot (Time Elapsing/Booked Room Color Fading):



Check out the codebase over here for more details: Conference Room Booker

Have you had experience with pure functional reactive programming using Cycle.js yet? If so, please share your experience in the comments or ask any questions you might have otherwise.

Wednesday, March 02, 2016

Cycle.js Application Structure (Conference Room Booker)

Added to Conference Room Booker HTTP requests and responses via Cycle.js HTTP driver, obtaining room names (e.g. France, Canada, etc...) and showing them within the floor map SVG.  



A side-effect to my work was refactoring the structure of the app, breaking components out of the main cycle function, primarily the following:

Sources:
- Models
  - roomCollection
- Streams
  - initialStream
Sinks:
- Views
  - bookingView
  - floorPlanView
Cycles (Both Sources & Sinks):
- roomRequest

Has anybody else been experimenting with Cycle.js's pure functional style of development?.  

Sunday, February 14, 2016

Ultra Light Wizard v0.1.0 (beta)

As promised, I rolled out a new update to Ultra Light Wizard (v0.1.0) that is finally feature complete, and thus labeled as beta. Please install, test, and report feedback on GitHub.

In the meantime, here are a few more tasks I'd like to tackle before it is v1.0.0 (production):

  • (must) Write automated tests
  • (optional) Support for nested resources
  • (optional) Modularize (perhaps extracting sub-generators)
  • (optional) General refactorings

Cheers and happy Rails coding!!

Saturday, February 13, 2016

Tuesday, February 09, 2016

Beyond React: Introducing Cycle.js

I'm giving a talk at the Montreal JS group tonight.

Title: Beyond React: Introducing Cycle.js

"Cycle.js is a reactive RxJS/Virtual DOM based front-end library. Andy will provide a quick introduction to Cycle.js basics and philosophies, go through a tutorial of its concepts and demonstrate with some code examples."

—Reference:

View Slides Online