回调方法应用 及 引入到模块方法应用

1: http://ruby-china.org/topics/413

module YourPlugin
  module Mixin
def xxx_attr(attr)
      attr = attr.to_s
      self.class_eval(%Q{
def #{attr}
  #...
end

def #{attr}=(value)
   #...
end
        }
      )
    end
  end
end

class Post
  include YourPlugin::Mixin
  xxx_attr :tag_list
end



2. http://quake.iteye.com/blog/448235

  1. #plugins/your_plugin_name/lib/enum_attr.rb
  2. moduleEnumAttr
  3. moduleMixin
  4. defenum_attr(attr,enums)
  5. attr=attr.to_s
  6. self.class_eval(%Q{
  7. ENUMS_#{attr.upcase}=enums
  8. validates_inclusion_ofattr,:in=>enums.map{|e|e[1]},:allow_nil=>true
  9. def#{attr}_name
  10. ENUMS_#{attr.upcase}.find{|option|option[1]==#{attr}}[0]unless#{attr}.nil?
  11. end
  12. })
  13. end
  14. end
  15. end
  16. #plugins/your_plugin_name/init.rb
  17. require'enum_attr'
  18. Object.send:include,EnumAttr::Mixin


你可能感兴趣的:(方法)