Ruby中鉤子方法的運用實例解析

這篇文章主要介紹了Ruby中鉤子方法的運用實例解析,鉤子方法的使用時Ruby入門學習中的基礎知識,需要的朋友可以參考下

通過使用鉤子方法,可以讓我們在Ruby的類或模塊的生命週期中進行干預,可以極大的提高編程的靈活性。
與生命週期相關的鉤子方法有下面這些:

類與模塊相關

  • Class#inherited
  • Module#include
  • Module#prepended
  • Module#extend_object
  • Module#method_added
  • Module#method_removed
  • Module#method_undefined

單件類相關

  • BasicObject#singleton_method_added
  • BasicObject#singleton_method_removed
  • BasicObject#singleton_method_undefined

示例代碼

module M1
  def self.included(othermod)
    puts “M1 was included into #{othermod}”
  end
end

module M2
  def self.prepended(othermod)
    puts “M2 was prepended to #{othermod}”
  end
end

class C
  include M1
  include M2
end

# 輸出
M1 was included into C
M2 was prepended to C

module M
  def self.method_added(method)
    puts “New method: M##{method}”
  end

  def my_method; end
end

# 輸出
New method: M#my_method

除了上面列出來的一些方法外,也可以通過重寫父類的某個方法,進行一些過濾操作後,再通過調用super方法完成原函數的功能,從而實現類似鉤子方法的功效,如出一轍,環繞別名也可以作爲一種鉤子方法的替代實現。

運用實例
任務描述:

寫一個操作方法類似attr_accessor的attr_checked的類宏,該類宏用來對屬性值做檢驗,使用方法如下:

class Person
 include CheckedAttributes

 attr_checked :age do |v|
  v >= 18
 end
end

me = Person.new
me.age = 39 #ok
me.age = 12 #拋出異常

實施計劃:

使用eval方法編寫一個名爲add_checked_attribute的內核方法,爲指定類添加經過簡單校驗的屬性
重構add_checked_attribute方法,去掉eval方法,改用其它手段實現
添加代碼塊校驗功能
修改add_checked_attribute爲要求的attr_checked,並使其對所有類都可用
通過引入模塊的方式,只對引入該功能模塊的類添加attr_checked方法
Step 1

def add_checked_attribute(klass, attribute)
 eval "
  class #{klass}
   def #{attribute}=(value)
    raise 'Invalid attribute' unless value
    @#{attribute} = value
   end
   def #{attribute}()
    @#{attribute}
   end
  end
 "
end

add_checked_attribute(String, :my_attr)
t = "hello,kitty"

t.my_attr = 100
puts t.my_attr

t.my_attr = false
puts t.my_attr

這一步使用eval方法,用class和def關鍵詞分別打開類,且定義了指定的屬性的get和set方法,其中的set方法會簡單的判斷值是否爲空(nil 或 false),如果是則拋出Invalid attribute異常。

Setp 2

def add_checked_attribute(klass, attribute)
 klass.class_eval do
  define_method "#{attribute}=" do |value|
   raise "Invaild attribute" unless value
   instance_variable_set("@#{attribute}", value)
  end

  define_method attribute do
   instance_variable_get "@#{attribute}"
  end

 end
end

這一步更換掉了eval方法,同時也分別用class_eval和define_method方法替換了之前的class與def關鍵字,實例變量的設置和獲取分別改用了instance_variable_set和instance_variable_get方法,使用上與第一步沒有任何區別,只是一些內部實現的差異。

Step 3

def add_checked_attribute(klass, attribute, &validation)
 klass.class_eval do
  define_method "#{attribute}=" do |value|
   raise "Invaild attribute" unless validation.call(value)
   instance_variable_set("@#{attribute}", value)
  end

  define_method attribute do
   instance_variable_get "@#{attribute}"
  end

 end
end

add_checked_attribute(String, :my_attr){|v| v >= 180 }
t = "hello,kitty"

t.my_attr = 100 #Invaild attribute (RuntimeError)
puts t.my_attr

t.my_attr = 200
puts t.my_attr #200

沒有什麼奇特的,只是加了通過代碼塊驗證,增加了校驗的靈活性,不再僅僅侷限於nil和false之間了。

Step 4

class Class
 def attr_checked(attribute, &validation)
   define_method "#{attribute}=" do |value|
    raise "Invaild attribute" unless validation.call(value)
    instance_variable_set("@#{attribute}", value)
   end

   define_method attribute do
    instance_variable_get "@#{attribute}"
   end
 end
end

String.add_checked(:my_attr){|v| v >= 180 }
t = "hello,kitty"

t.my_attr = 100 #Invaild attribute (RuntimeError)
puts t.my_attr

t.my_attr = 200
puts t.my_attr #200

這裏我們把之前頂級作用域中方法名放到了Class中,由於所有對象都是Class的實例, 所以這裏定義的實例方法,也能被Ruby中的其它所有類訪問到,同時在class定義中,self就是當前類,所以也就省去了調用類這個參數和class_eval方法,並且我們把方法的名字也改成了attr_checked。

Step 5

module CheckedAttributes
 def self.included(base)
  base.extend ClassMethods
 end
end

module ClassMethods
 def attr_checked(attribute, &validation)
   define_method "#{attribute}=" do |value|
    raise "Invaild attribute" unless validation.call(value)
    instance_variable_set("@#{attribute}", value)
   end

   define_method attribute do
    instance_variable_get "@#{attribute}"
   end
 end
end

class Person
 include CheckedAttributes

 attr_checked :age do |v|
  v >= 18
 end
end

最後一步通過鉤子方法,在CheckedAttributes模塊被引入後,對當前類通過被引入模塊進行擴展, 從而使當前類支持引入後的方法調用,即這裏的get與set方法組。

到此,我們已經得到了一個名爲attr_checked,類似attr_accessor的類宏,通過它你可以對屬性進行你想要的校驗。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章