Sha256: 90c40f85a32653b1434eec6890f39893861b67577dd68b87dda4369080a1f27e

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

require 'action_controller'

module Zertico
  class Controller < ActionController::Base
    def initialize
      begin
        extend "::#{self.class.name.chomp('Controller').concat('Service')}".constantize
      rescue NameError
        extend Zertico::Service
      end
      super
    end

    def index
      initialize_object all
      respond_with(instance_variable_get('@responder'))
    end

    def new
      initialize_object build
      respond_with(instance_variable_get('@responder'))
    end

    def show
      initialize_object find(params[interface_id.to_sym])
      respond_with(instance_variable_get('@responder'))
    end

    def edit
      initialize_object find(params[interface_id.to_sym])
      respond_with(instance_variable_get('@responder'))
    end

    def create
      initialize_object generate(params[interface_name.to_sym])
      if instance_variable_defined?('@location')
        respond_with(instance_variable_get('@responder'), :location => instance_variable_get('@location'))
      else
        respond_with(instance_variable_get('@responder'))
      end
    end

    def update
      initialize_object modify(params[interface_id.to_sym], params[interface_name.to_sym])
      if instance_variable_defined?('@location')
        respond_with(instance_variable_get('@responder'), :location => instance_variable_get('@location'))
      else
        respond_with(instance_variable_get('@responder'))
      end
    end

    def destroy
      initialize_object delete(params[interface_id.to_sym])
      if instance_variable_defined?('@location')
        respond_with(instance_variable_get('@responder'), :location => instance_variable_get('@location'))
      else
        respond_with(instance_variable_get('@responder'))
      end
    end

    protected

    def initialize_object(objects = {})
      objects.each do |key, value|
        instance_variable_set("@#{key}", value)
        instance_variable_set('@responder', value) unless @responder
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zertico-0.6.1 lib/zertico/controller.rb