Sha256: 42e1b00256841adea601b66b475f8dac860e229c31c6021fd7744e4a6574cdd1

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

require "active_support/inflector"
require "pp"

module Peto
  module RailsControllerHelper
    def contract
      self.class.to_s.sub("Controller", "")
    end

    def peto_class
      "Peto::#{contract.to_s.camelize}".constantize
    end

    def parse_caller(at)
      if /^(.+?):(\d+)(?::in `(.*)')?/ =~ at
        {:file => $1, :line => $2.to_i, :method => $3.to_sym}
      end
    end


    # respond

    def procedure_name
      parse_caller(caller[(0..caller.length).zip(caller).find {|index,at|
        parse_caller(at)[:method] == :call_subaction
      }.first - 1])[:method]
    end

    def call_procedure_response(response_type, *args)
      peto_class.send(:"#{procedure_name}_#{response_type}", *args)
    end

    def respond(*args)
      render(:json => call_procedure_response(:response, *args).to_json)
    end

    def error(error_name, *messages)
      render(:json => call_procedure_response(:"error_#{error_name}", messages.join(",")))
    end


    # subaction

    def index
      call_subaction(params[:procedure], params[:args])
    end

    def valid_args?(procedure, args)
      peto_class.send(:"#{procedure}_valid?", args)
    end

    def hash_to_args(procedure, hash_args)
      peto_class.send(:"#{procedure}_hash_to_args", hash_args)
    end

    def call_subaction(procedure, hash_args)
      args = hash_to_args(procedure, hash_args)
      valid_args?(procedure, *args) # raise error
      send(procedure, *args)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
peto-0.2.7 lib/peto/rails/rails_controller_helper.rb
peto-0.2.6 lib/peto/rails/rails_controller_helper.rb
peto-0.2.5 lib/peto/rails/rails_controller_helper.rb