# File lib/base_object.rb, line 190 def initialize(opts={}, &block) super(opts) @action_urls = opts[:action_urls] || [] @actions = [] end
This trigger is called right after action. This method does nothing inside ActionObject but it can be redifined and used in meta-programming
# File lib/base_object.rb, line 199 def action_trigger(action) end
# File lib/base_object.rb, line 223 def action_urls actions.collect { |a| a.last } end
# File lib/base_object.rb, line 216 def actions @objects.inject([]) do |result, item| result << [item[:rel], item[:href]] if item[:type].eql?(:action_link) result end end
# File lib/base_object.rb, line 202 def add_action_link!(id, link) m = { :type => :action_link, :method_name => "#{link['rel'].sanitize}!", :id => id, :href => link['href'], :rel => link['rel'].sanitize, :method => link['method'].sanitize } @objects << m @actions << [m[:rel], m[:href]] @action_urls << m[:href] end
First call BaseObject method handler, then, if not method found try ActionObject handler
# File lib/base_object.rb, line 231 def method_handler(m, args=[]) begin base_method_handler(m, args) rescue NoHandlerForMethod case m[:type] when :action_link then do_action(m, args) else raise NoHandlerForMethod end end end
# File lib/base_object.rb, line 244 def method_missing(name, *args) if name.to_s =~ /^has_(\w+)\?$/ return actions.any? { |a| a[0] == $1 } end original_method_missing(name, args) end
# File lib/base_object.rb, line 253 def do_action(m, args) args = args.first || {} method = m[:method].to_sym @client.request(method, m[:href], method == :get ? args : {}, method == :get ? {} : args) action_trigger(m[:rel]) end