Slide 1

Slide 1 text

CHG Chanko Hacking Guide @r7kamura

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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.

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

rails >= 3.0.10 chanko.gemspec

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

.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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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?

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Thank you github.com/cookpad/chanko