inactivesupport

38
株式会社永和システムマネジメント サービスプロバイディング事業部 浦嶌 啓太 InactiveSupportのご紹介

Upload: keita-urashima

Post on 12-Jul-2015

997 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: InactiveSupport

株式会社永和システムマネジメントサービスプロバイディング事業部

浦嶌 啓太

InactiveSupportのご紹介

Page 2: InactiveSupport

浦嶌 啓太http://twitter.com/ursm/

Page 3: InactiveSupport

http://d.hatena.ne.jp/ursm/

Page 4: InactiveSupport

http://haml.ursm.jp

Page 5: InactiveSupport
Page 6: InactiveSupport

よろしくお願いします

Page 7: InactiveSupport
Page 8: InactiveSupport

本日お話しすること•うちのプロジェクトのInactiveSupportを皆さんにご紹介します!

Page 9: InactiveSupport

•特定のライブラリというわけではなくて•プロジェクトを進めていくうちに溜まっていったdirty hackの集まりを勝手にこう呼んでいる•本来は人様にお見せするようなものではない

InactiveSupportって?

Page 10: InactiveSupport

警告本LTは、あなたのコードの可読性・メンテナンス性を損う恐れがあります

Page 11: InactiveSupport

•Object#tapp•Hash#to_struct•Hash#pairs_at•Object#try•Object#irb

お品書き

Page 12: InactiveSupport

Object#tapp•selfをppしてselfを返す

>> 'foo'.tapp"foo"=> "foo"

Page 13: InactiveSupport

class Object def tapp require 'pp' tap { pp self } endend

Page 14: InactiveSupport

(1..3).map {|i| i ** 2}.inject(&:+)

tmp = (1..3).map {|i| i ** 2}

pp tmp

tmp.inject(&:+)

Page 15: InactiveSupport

(1..3).map {|i| i ** 2}.tap {|v| pp v }.inject(&:+)

(1..3).map {|i| i ** 2}.tapp.inject(&:+)

Page 16: InactiveSupport

Hash#to_struct•Hashを再帰的にStructに変換する

>> s = {:foo => {:bar => 1}}.to_struct=> #<OpenStruct foo=#<OpenStruct bar=1>>

>> s.foo.bar=> 1

Page 17: InactiveSupport

require 'ostruct'

class Hash def to_struct OpenStruct.new(inject({}) {|h, (k, v)| h.merge(k => v.is_a?(Hash) ? v.to_struct : v) }) endend

Page 18: InactiveSupport

CONFIG[:admin][:name]

CONFIG = { :admin => { :name => 'yasuo' }, ...}

Page 19: InactiveSupport

CONFIG.admin.name

CONFIG = OpenStruct.new( :admin => OpenStruct.new( :name => 'yasuo' ), ...)

Page 20: InactiveSupport

CONFIG.admin.name

CONFIG = { :admin => { :name => 'yasuo' }, ...}.to_struct

Page 21: InactiveSupport

参考✓Configatronというライブラリがあります✓設定格納用オブジェクトを提供する✓構造体っぽくアクセスできる✓Railsプラグインもある

Page 22: InactiveSupport

Hash#pairs_at•あるHashのうち、指定したキーのペアからなるHashを生成して返す

>> h = {:a => 1, :b => 2, :c => 3}=> {:a=>1, :b=>2, :c=>3}

>> h.pairs_at(:a, :c)=> {:a=>1, :c=>3}

Page 23: InactiveSupport

class Hash def pairs_at(*keys) keys.zip(values_at(*keys)).inject(self.class.new) {|h, (k, v)| h.merge(k => v) } endend

Page 24: InactiveSupport

class UsersController def create u = User.new(params[:user]) ... endend

Page 25: InactiveSupport

class UsersController def create u = User.new( :name => params[:user][:name], :age => params[:user][:age] ) ... endend

Page 26: InactiveSupport

class UsersController def create u = User.new( params[:user].pairs_at(:name, :age) ) ... endend

Page 27: InactiveSupport

class UsersController def create u = User.new( params[:user].slice(:name, :age) ) ... endend

Caution!•Rails 2.1から同様の機能を提供するHash#sliceが入りました

Page 28: InactiveSupport

Object#try•selfが指定したメソッドを持っているときはそのまま実行する•持っていないときはnilを返す

>> 'foo'.try(:capitalize)=> "Foo"

>> 'foo'.try(:hoge)=> nil

Page 29: InactiveSupport

class Object def try(method, *args, &block) respond_to?(method) ? send(method, *args, &block) : nil endend

Page 30: InactiveSupport

<%=h @user.sector.name %>

<%=h @user.sector && @user.sector.name %>

<%=h @user.sector.try(:name) %>

Page 31: InactiveSupport

•Rails 2.3から同名のメソッドが入りました•selfがnilのときだけnilを返すようになっています

Caution!

>> nil.try(:hoge)=> nil

>> 1.try(:hoge)NoMethodError: undefined method `hoge' for 1:Fixnum

Page 32: InactiveSupport

Object#irb•任意のオブジェクトをコンテキストにしてirbを起動する

class UsersController def create irb ... endend

Page 33: InactiveSupport

class Object def irb require 'irb' IRB.setup nil IRB::Irb.new(IRB::WorkSpace.new(binding)).tap {|i| IRB.conf[:MAIN_CONTEXT] = i.context }.eval_input endend

Page 34: InactiveSupport

Caution!•それruby-debugでできるよ

Page 35: InactiveSupport

余談•what_methodsというライブラリが便利です

>> 'users'.what? 'User'"users".classify == "User"=> ["classify"]>> 'User'.what? 'users'"User".tableize == "users"=> ["tableize"]

Page 36: InactiveSupport

InactiveSupportの心得✓やりすぎない✓複雑なことをしない✓ドメインロジックに踏み込まない✓既存メソッドの再定義は禁じ手

Page 37: InactiveSupport

まとめ✓Rubyは自由で危険な言語です✓そんなRubyが私は大好きです

http://gist.github.com/154715

Page 38: InactiveSupport

ご清聴ありがとうございました