Sha256: 063684ed72b00a8120157348f12b05b553c9a12a31472608930082ffacf4738b

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

module Zertico
  module Service
    def all
      @responder = resource.all
      instance_variable_set("@#{interface_name.pluralize}", @responder)
      @options = {}
    end

    def build
      @responder = resource.new
      instance_variable_set("@#{interface_name}", @responder)
      @options = {}
    end

    def find
      @responder = resource.find(params[interface_id.to_sym])
      instance_variable_set("@#{interface_name}", @responder)
      @options = {}
    end

    def generate
      @responder = resource.create(params[interface_name.to_sym])
      instance_variable_set("@#{interface_name}", @responder)
      @options = {}
    end

    def modify
      find
      @responder.update_attributes(params[interface_name.to_sym])
      @options = {}
    end

    def delete
      find
      @responder.destroy
      @options = {}
    end

    def resource
      @resource ||= interface_class
    end

    def resource=(resource_chain = [])
      @resource = resource_chain.shift
      @resource = @resource.constantize if @resource.respond_to?(:constantize)
      resource_chain.each do |resource|
        @resource = @resource.send(resource)
      end
    end

    protected

    def interface_id
      return "#{interface_name}_id" if self.class.name.chomp('Controller').split('::').size > 1
      'id'
    end

    def interface_name
      self.interface_class.name.singularize.underscore
    end

    def interface_class
      begin
        self.class.name.chomp('Controller').singularize.constantize
      rescue NameError
        self.class.name.chomp('Controller').split('::').last.singularize.constantize
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zertico-1.0.0 lib/zertico/service.rb