$30 off During Our Annual Pro Sale. View Details »

Chanko Hacking Guide

r7kamura
April 28, 2014

Chanko Hacking Guide

r7kamura

April 28, 2014
Tweet

More Decks by r7kamura

Other Decks in Programming

Transcript

  1. CHG
    Chanko Hacking Guide
    @r7kamura

    View Slide

  2. 1. README.md
    2. chanko.gemspec
    3. lib/chanko.rb
    4. lib/chanko/railtie.rb
    5. lib/chanko/invoker.rb
    6. lib/chanko/loader.rb
    7. lib/chanko/function.rb

    View Slide

  3. README.md
    Chanko
    !
    Chanko provides a simple framework for rapidly and safely
    prototyping new features in your production Rails app, and
    exposing these prototypes to specified segments of your user base.
    !
    With Chanko, you can release many features concurrently and
    manage target users independently. When any errors are raised from
    chanko's features, it will be automatically hidden and fallback to its
    normal behavior.

    View Slide

  4. README.md
    = invoke(:unit_name, :function_name) do
    # fallback …

    View Slide

  5. rails >= 3.0.10
    chanko.gemspec

    View Slide

  6. Bundler.require
    Loads all Gems listed in Gemfile.
    Open your config/application.rb
    to see the real example.
    !
    # example
    Bundler.require(:default, Rails.env)

    View Slide

  7. require "action_controller"
    require "action_view"
    require "active_record"
    require "active_support/all"
    require "rails"
    require "chanko/active_if"
    require "chanko/config"
    require "chanko/controller"
    require "chanko/exception_handler"
    require "chanko/function"
    require "chanko/helper"
    require "chanko/invoker"
    require "chanko/loader"
    require "chanko/logger"
    require "chanko/railtie"
    require "chanko/unit"
    require "chanko/unit_proxy"
    require "chanko/unit_proxy_provider"
    lib/chanko.rb
    external
    internal

    View Slide

  8. Rails::Railtie
    Railtie is the core of the Rails framework and
    provides several hooks to extend Rails and/or
    modify the initialization process.
    !
    class MyRailtie < Rails::Railtie
    initializer "my_initializer_name" do
    # some initialization behavior
    end
    end

    View Slide

  9. Rails Init Process
    1. `rails s`
    2. bin/rails
    3. rails/commands/server … or using unicorn
    4. config.ru
    5. config/application.rb
    6. Bundler.require
    7. My::Application.initialize!
    8. initializers.tsort_each…
    9. run My::Application

    View Slide

  10. initializer "chanko" do
    ActionController::Base.include Controller
    ActionController::Base.include Invoker
    ActionController::Base.include UnitProxyProvider
    ActionView::Base.include Helper
    ActionView::Base.include Invoker
    ActionView::Base.include UnitProxyProvider
    end
    lib/chanko/railtie.rb

    View Slide

  11. .invoke(:x, :y)
    1. find a module named “X”
    2. find a function named “y” from “X”
    3. stack local variables
    4. call “y”
    5. unstack local variables
    6. call fallback block if any error occurred
    lib/chanko/invoker.rb

    View Slide

  12. Loader.load(name)
    1. autoload_paths << app/extensions/#{name}
    2. name.camelize.constantize
    3. rescue Exception
    lib/chanko/loader.rb

    View Slide

  13. AS::Dependencies
    1. Register auto load path
    2. Find module if const_missing occurred
    3. Clear loaded modules if any file changed

    View Slide

  14. lib/chanko/invoker.rb
    view.invoke(:x, :y)
    Loader.load(:x)
    AS::Dependencies
    “X”.constantize
    Loader.cache
    .clear
    in each request
    in development

    View Slide

  15. lib/chanko/invoker.rb
    view.invoke(:x, :y)
    X.find_function(view, :y)
    function.invoke(view)
    view.instance_eval
    X.scopes[view][:y]
    module X
    scopes(:view) {
    function(:y) {

    }
    }
    end
    function.active?

    View Slide

  16. lib/chanko/function.rb
    instance
    _eval
    decoration
    exception handler
    view path
    stack
    unit stack
    fallback stack

    View Slide

  17. Thank you
    github.com/cookpad/chanko

    View Slide