Wednesday, 20 August 2014
Wednesday, 13 August 2014
Monday, 11 August 2014
Proxy
http://blog.rubybestpractices.com/posts/gregory/060-issue-26-structural-design-patterns.html
Proxy
A Proxy is any object that acts as a drop-in replacement object that does a bit of work and then delegates to some other underlying object. This is another pattern that’s easy to recognize for Rails developers, because it is used extensively in ActiveRecord associations support. The following example shows a typical interaction between a base model and one of its associations.
- quiz = Quiz.first
- quiz.questions.class #=> Array
- quiz.questions.count #=> 10
- quiz.questions.create(:question_text => "Who is the creator of Ruby?",
- :answer => "Matz")
- quiz.questions.count #=> 11
- quiz.questions[-1].answer #=> "Matz"
While the questions association claims to be an array, it also provides some of the common helpers found in ActiveRecord::Base. If we stick to the core idea and ignore some of the Rails specific details, creating such an association proxy is fairly easy to do using Ruby’s delegate standard library. The code below more or less does the trick.
- require "delegate"
- class Quiz
- def questions
- @questions ||= HasManyAssociation.new([])
- @questions.associated_class ||= Question
- @questions
- end
- end
- class Question
- def initialize(params)
- @params = params
- end
- attr_reader :params
- def answer
- params[:answer]
- end
- end
- class HasManyAssociation < DelegateClass(Array)
- attr_accessor :associated_class
- def initialize(array)
- super(array)
- end
- def create(params)
- self << associated_class.new(params)
- end
- end
- quiz = Quiz.new
- # grab the proxy object
- questions = quiz.questions
- # use custom create() method on proxy object
- questions.create(:question_text => "Who is the creator of Ruby?",
- :answer => "Matz")
- questions.create(:question_text => "Who is the creator of Perl?",
- :answer => "Larry Wall")
- # use array like behavior on proxy object
- p questions[0].answer #=> "Matz"
- p questions[1].answer #=> "Larry Wall"
- p questions.map { |q| q.answer }.join(", ") #=> "Matz, Larry Wall"
Interestingly enough, while Ruby provides a standard library for building Proxy objects, most people tend to implement them in a different way, through the use of an explicitmethod_missing() hook on a blank slate object such as Ruby 1.9’s BasicObject. For example, we could have written our HasManyAssociation code in the manner shown below and things would still work as expected.
- class HasManyAssociation < BasicObject
- attr_accessor :associated_class
- def initialize(array)
- @array = array
- end
- def create(params)
- self << associated_class.new(params)
- end
- def method_missing(*a, &b)
- @array.send(*a, &b)
- end
- end
Without looking at the source, I’m almost sure that Rails does something similar to this, because doing some_association.class returns Array rather than the name of the proxy object. This is the only noticeable difference on the surface between this approach and the DelegateClass approach.
Personally, I’ve written proxies in both ways, and I tend to prefer the DelegateClass()approach slightly, simply because it’s more explicit and doesn’t require me to explicitly define amethod_missing() hook. But on the other hand, we can see that rolling your own proxy implementation is trivial in Ruby, and some may prefer the self contained nature of doing the delegation work yourself. It’d be interesting to hear what readers have to say on this topic, so please feel free to post to the mailing list if you prefer one approach over the other.
Thursday, 24 July 2014
activesupportconcern
FROM: http://engineering.appfolio.com/2013/06/17/ruby-mixins-activesupportconcern/
Ruby Mixins & ActiveSupport::Concern
A few people have asked: what is the dealio with ActiveSupport::Concern? My answer: it encapsulates a few common patterns for building modules intended for mixins. Before understanding why ActiveSupport::Concern is useful, we first need to understand Ruby mixins.
Here we go…!
First, the Ruby object model:
http://blog.jcoglan.com/2013/05/08/how-ruby-method-dispatch-works/
http://www.ruby-doc.org/core-1.9.3/Class.html
http://www.ruby-doc.org/core-1.9.3/Class.html
As you can see mixins are “virtual classes” that have been injected in a class’s or module’s ancestor chain. That is, this:
module MyMod end class Base end class Child < Base include MyMod end # irb> Child.ancestors # => [Child, MyMod, Base, Object, Kernel, BasicObject]
results in the same ancestor chain as this:
class Base end class MyMod < Base end class Child < MyMod end # irb> Child.ancestors # => [Child, MyMod, Base, Object, Kernel, BasicObject]
Great? Great.
Modules can also be used to extend objects, for example:
my_obj = Object.new my_obj.extend MyMod # irb> my_obj.singleton_class.ancestors # => [MyMod, Object, Kernel, BasicObject]
In Ruby, every object has a singleton class. Object#extend does what Module#include does but on an object’s singleton class. That is, the following is equivalent to the above:
my_obj = Object.new my_obj.singleton_class.class_eval do include MyMod end # irb> my_obj.singleton_class.ancestors # => [MyMod, Object, Kernel, BasicObject]
This is how “static” or “class” methods work in Ruby. Actually, there’s no such thing as static/class methods in Ruby. Rather, there are methods on a class’s singleton class. For example, w.r.t. the ancestors chain, the following are equivalent:
class MyClass
extend MyMod
end
# irb> MyClass.singleton_class.ancestors
# => [MyMod, Class, Module, Object, Kernel, BasicObject]
class MyClass
class << self
include MyMod
end
end
# irb> MyClass.singleton_class.ancestors
# => [MyMod, Class, Module, Object, Kernel, BasicObject]
Classes are just objects “acting” as “classes”.
Back to mixins…
Ruby provides some hooks for modules when they are being mixed into classes/modules:
http://www.ruby-doc.org/core-1.9.3/Module.html#method-i-included
http://www.ruby-doc.org/core-1.9.3/Module.html#method-i-extended
http://www.ruby-doc.org/core-1.9.3/Module.html#method-i-included
http://www.ruby-doc.org/core-1.9.3/Module.html#method-i-extended
For example:
module MyMod
def self.included(target)
puts "included into #{target}"
end
def self.extended(target)
puts "extended into #{target}"
end
end
class MyClass
include MyMod
end
# irb>
# included into MyClass
class MyClass2
extend MyMod
end
# irb>
# extended into MyClass2
# irb> MyClass.ancestors
# => [MyClass, MyMod, Object, Kernel, BasicObject]
# irb> MyClass.singleton_class.ancestors
# => [Class, Module, Object, Kernel, BasicObject]
# irb> MyClass2.ancestors
# => [MyClass2, Object, Kernel, BasicObject]
# irb> MyClass2.singleton_class.ancestors
# => [MyMod, Class, Module, Object, Kernel, BasicObject]
Great? Great.
Back to ActiveSupport::Concern…
Over time it became a common pattern in the Ruby worldz to create modules intended for use as mixins like this:
module MyMod
def self.included(target)
target.send(:include, InstanceMethods)
target.extend ClassMethods
target.class_eval do
a_class_method
end
end
module InstanceMethods
def an_instance_method
end
end
module ClassMethods
def a_class_method
puts "a_class_method called"
end
end
end
class MyClass
include MyMod
# irb> end
# a_class_method called
end
# irb> MyClass.ancestors
# => [MyClass, MyMod::InstanceMethods, MyMod, Object, Kernel, BasicObject]
# irb> MyClass.singleton_class.ancestors
# => [MyMod::ClassMethods, Class, Module, Object, Kernel, BasicObject]
As you can see, this single module is adding instance methods, “class” methods, and acting directly on the target class (calling a_class_method() in this case).
ActiveSupport::Concern encapsulates this pattern. Here’s the same module rewritten to use ActiveSupport::Concern:
module MyMod
extend ActiveSupport::Concern
included do
a_class_method
end
def an_instance_method
end
module ClassMethods
def a_class_method
puts "a_class_method called"
end
end
end
You’ll notice the nested InstanceMethods is removed and an_instance_method() is defined directly on the module. This is because this is standard Ruby; given the ancestors of MyClass ([MyClass, MyMod::InstanceMethods, MyMod, Object, Kernel]) there’s no need for a MyMod::InstanceMethods since methods on MyMod are already in the chain.
So far ActiveSupport::Concern has taken away some of the boilerplate code used in the pattern: no need to define an included hook, no need to extend the target class with ClassMethods, no need to class_eval on the target class.
The last thing that ActiveSupport::Concern does is what I call lazy evaluation. What’s that?
Back to Ruby mixins…
Consider:
module MyModA end module MyModB include MyModA end class MyClass include MyModB end # irb> MyClass.ancestors # => [MyClass, MyModB, MyModA, Object, Kernel, BasicObject]
Let’s say MyModA wanted to do something special when included into the target class, say:
module MyModA
def self.included(target)
target.class_eval do
has_many :squirrels
end
end
end
When MyModA is included in MyModB, the code in the included() hook will run, and if has_many() is not defined on MyModB things will break:
irb :050 > module MyModB irb :051?> include MyModA irb :052?> end NoMethodError: undefined method `has_many' for MyModB:Module from (irb):46:in `included' from (irb):45:in `class_eval' from (irb):45:in `included' from (irb):51:in `include'
ActiveSupport::Concern skirts around this issue by delaying all the included hooks from running until a module is included into a non-ActiveSupport::Concern. Redefining the above using ActiveSupport::Concern:
module MyModA
extend ActiveSupport::Concern
included do
has_many :squirrels
end
end
module MyModB
extend ActiveSupport::Concern
include MyModA
end
class MyClass
def self.has_many(*args)
puts "has_many(#{args.inspect}) called"
end
include MyModB
# irb>
# has_many([:squirrels]) called
end
# irb> MyClass.ancestors
# => [MyClass, MyModB, MyModA, Object, Kernel, BasicObject]
# irb> MyClass.singleton_class.ancestors
# => [Class, Module, Object, Kernel, BasicObject]
Great? Great.
But why is ActiveSupport::Concern called “Concern”? The name Concern comes from AOP (http://en.wikipedia.org/wiki/Aspect-oriented_programming). Concerns in AOP encapsulate a “cohesive area of functionality”. Mixins act as Concerns when they provide cohesive chunks of functionality to the target class. Turns out using mixins in this fashion is a very common practice.
ActiveSupport::Concern provides the mechanics to encapsulate a cohesive chunk of functionality into a mixin that can extend the behavior of the target class by annotating the class’ ancestor chain, annotating the class’ singleton class’ ancestor chain, and directly manipulating the target class through the included() hook.
So….
Is every mixin a Concern? No. Is every ActiveSupport::Concern a Concern? No.
While I’ve used ActiveSupport::Concern to build actual Concerns, I’ve also used it to avoid writing out the boilerplate code mentioned above. If I just need to share some instance methods and nothing else, then I’ll use a bare module.
Modules, mixins and ActiveSupport::Concern are just tools in your toolbox to accomplish the task at hand. It’s up to you to know how the tools work and when to use them.
I hope that helps somebody.
Thursday, 17 July 2014
Thursday, 3 July 2014
Subscribe to:
Posts (Atom)