Sha256: 82621b94ce3bfe502028638cd823ac6f9b036ab5cedec2041bf7ded57ff4f227

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

module Restfulie
  
  module Server

    # represents a transition on the server side
    class Transition
      attr_reader :body, :name
      attr_writer :options
      attr_accessor :result
      def initialize(name, options = {}, result = nil, body = nil)
        @name = name
        @options = options
        @result = result
        @body = body
      end
      def action
        @options || {}
      end
      
      # executes this transition in a resource
      def execute_at(target_object)
        target_object.status = result.to_s unless result.nil?
      end
    
      # adds a link to this transition's uri on a xml writer
      def add_link_to(xml, model, options)
        specific_action = action.dup
        specific_action = @body.call(model) if @body

        # if you use the class level DSL, you will need to add a lambda for instance level accessors:
        #   transition :show, {:action => :show, :foo_id => lambda { |model| model.id }}
        # but you can replace it for a symbol and defer the model call
        #   transition :show, {:action => :show, :foo_id => :id}
        specific_action = specific_action.inject({}) do |actions, pair|
          if pair.last.is_a?( Symbol ) && model.attributes.include?(pair.last)
            actions.merge!( pair.first => model.send(pair.last) )
          else
            actions.merge!( pair.first => pair.last )
          end
        end

        rel = specific_action[:rel] || @name
        specific_action[:rel] = nil

        specific_action[:action] ||= @name
        uri = options[:controller].url_for(specific_action)
      
        if options[:use_name_based_link]
          xml.tag!(rel, uri)
        else
          xml.tag!('atom:link', 'xmlns:atom' => 'http://www.w3.org/2005/Atom', :rel => rel, :href => uri)
        end
      end
      
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
restfulie-0.4.0 lib/restfulie/server/transition.rb