Upgrade to Pro — share decks privately, control downloads, hide ads and more …

opal beginning

opal beginning

Fumiaki MATSUSHIMA

May 15, 2015
Tweet

More Decks by Fumiaki MATSUSHIMA

Other Decks in Programming

Transcript

  1. source 'https://rubygems.org' gem 'activesupport' gem 'opal' gem 'opal-jquery' gem 'pry-byebug'

    gem 'slim' gem 'sprockets' source 'https://rails-assets.org' do gem 'rails-assets-jquery' end Gemfile
  2. require 'bundler' Bundler.require map '/assets' do environment = Sprockets::Environment.new environment.append_path

    'app/js' RailsAssets.load_paths.each {|p| environment.append_path(p) } Opal.paths.each {|p| environment.append_path(p) } run environment end map '/' do environment = Sprockets::Environment.new environment.append_path 'app/html' environment.register_engine '.slim', Slim::Template run environment end config.ru
  3. doctype html html head title opal sample script(src='assets/opal.js') script(src='assets/jquery.js') script(src='assets/opal-jquery.js')

    script(src='assets/opal-parser.js') body button | click me script(type='text/ruby') | Document.ready? do Element.find('button').on(:click) do |evt| h1 = Element.new(:h1) h1.text = 'Hello world' Element.find('body').append(h1) evt.current_target.remove end end
  4. require 'bundler' Bundler.require map '/assets' do environment = Sprockets::Environment.new environment.append_path

    'app/js' RailsAssets.load_paths.each {|p| environment.append_path(p) } Opal.use_gem 'opal-jquery' Opal.paths.each {|p| environment.append_path(p) } run environment end map '/' do environment = Sprockets::Environment.new environment.append_path 'app/html' environment.register_engine '.slim', Slim::Template run environment end config.ru
  5. require 'bundler' Bundler.require map '/assets' do environment = Sprockets::Environment.new environment.append_path

    'app/js' RailsAssets.load_paths.each {|p| environment.append_path(p) } Opal.use_gem 'activesupport' Opal.use_gem 'opal-jquery' Opal.paths.each {|p| environment.append_path(p) } run environment end map '/' do environment = Sprockets::Environment.new environment.append_path 'app/html' environment.register_engine '.slim', Slim::Template run environment end config.ru
  6. doctype html html head title opal sample script(src='assets/opal.js') script(src='assets/jquery.js') script(src='assets/opal-jquery.js')

    script(src='assets/active_support/core_ext/object/blank.js') body .ruby nil.blank? .opal opal: Element.find('.opal').text = nil.blank?
  7. doctype html html head title opal sample script(src='assets/opal.js') script(src='assets/jquery.js') script(src='assets/opal-jquery.js')

    script(src='assets/active_support/all.js') body .ruby ActiveSupport::NumberHelper.number_to_human_size(1234) .opal opal: Element.find('.opal').text = ActiveSupport::NumberHelper.number_to_human_size(1234)
  8. class BigDecimal class << self def new(val) `Number(#{val})` end end

    end def BigDecimal(value) BigDecimal.new(value) end `` で直接 js
  9. module ActiveSupport module NumberHelper class NumberToDelimitedConverter private def parts left,

    right = number.to_s.split('.') left = left.gsub(DELIMITED_REGEX) do |digit_to_delimit| "#{digit_to_delimit}#{options[:delimiter]}" end [left, right].compact end end end end gsub! -> gsub