Skip to content

Instantly share code, notes, and snippets.

View adambeynon's full-sized avatar

Adam Beynon adambeynon

View GitHub Profile
@adambeynon
adambeynon / blog.md
Last active August 29, 2015 14:19
Keeping Code DRY With Opal

** Draft **

I develop and maintain several apps written using Opal. These vary in their size, and vary in their usage. Each of these apps use rails as a backend. Some use html rendered through rails, and some use json rendered by rails. But in each case, these apps are tied in logic to the backend, and indeed share model logic between client and server.

  • Don't Repeat Yourself.
  • Shared modules - code once, use client & server
  • RSpec - write tests once, run client & server
  • Maintainability - Opal, questions about maintenance. Offset against maintaining ruby + coffeescript versions of same code
  • Overhead - filesize vs. context of code, is a little filesize worse than maintaing 2 code bases
  • Opal -> js, no different from coffeescript/es6 -> js
Opal.build_manifest do
# find application.rb in our load path and compile that (and all dependencies)
compile "application"
# we want all templates included, but use our custom haml (react.js style) parser
each_glob "templates/**/*.haml" do |file|
special_haml_compile file
end
# compile these default gems that might not be required inside application.rb
@adambeynon
adambeynon / fire.rb
Created October 23, 2014 16:39
react to fire
Router.map do
route :index do
index :inbox_stats
route 'message/:id'
end
route :calendar
index :dashboard
end
class AvailabilityChecker
def initialize(reservation)
@reservation = reservation
end
def available?
if @reservation.new_record?
new_record_available?
elsif @reservation.unit_id_changed?
update_unit_available?
@adambeynon
adambeynon / ugly.js
Created June 7, 2014 12:08
ugly code from beautiful code
// ugly code from beautiful code
// object.foo(1, 2, 3)
(_a = object, _a.$foo || $mm)(_a, 'foo', 1, 2, 3);
@adambeynon
adambeynon / bindings.rb
Created May 7, 2014 15:58
Opal Bound attributes + DOM Compiler
module Vienna
class BaseBinding
def initialize(view, context, node, attr)
@view = view
@context = context
@node = node
@attr = attr
setup_binding
end
require 'opal-jquery/version'
if RUBY_ENGINE == "opal"
require 'opal-jquery/window'
require 'opal-jquery/document'
require 'opal-jquery/element'
require 'opal-jquery/event'
require 'opal-jquery/http'
require 'opal-jquery/kernel'
end

Sending method no args

recv.foo
rb_send0(recv, 'foo');

function rb_send0(recv, mid) {
RACSignal *nameSignal = [RACObserve(self, someManagedObject.name) distinctUntilChanged];
RAC(self, someTextField.text) = [nameSignal deliverOn:[RACScheduler mainThreadScheduler]];
signal = observe(self, "some_managed_object.name")
signal.on_value { |val| self.some_text_field.text = val }

The idea is that we are binding an attribute cart_empty on the current object to get updated when some remote attribute on some other object changes. This results in a method call current_obj.cart_empty = new_value.

bind(:cart_empty).to observe(@cart, :empty?)

bind(:cart_empty).to @cart.observe(:empty?)

bind :cart_empty, @cart.observe(:empty?)