Sha256: af9299ee998fd0426ab397ab42589b46617c86383f982616a8d73cfc37e8e41a

Contents?: true

Size: 1.77 KB

Versions: 19

Compression:

Stored size: 1.77 KB

Contents

module EitilWrapper
  module ControllerDecorator

    private

    def decorate(dec_item, dec_method: nil, dec_class: nil, **dec_kwargs)
      all_args_to_ivars binding
      set_ivars :dec_class, :decorator, :dec_method
      send_to_decorator

      rescue NameError => e
        puts "EitilWrapper::ControllerDecorator => rescuing uninitialised constant #{e.name}, returning the object's default JSON instead."
        return dec_item.as_json
    end

    def send_to_decorator
       @decorator.send(@dec_method, @dec_item)
     
      rescue NoMethodError => e       
        inform_no_method_for_decorator_error
        @dec_item
    end

    def inform_no_method_for_decorator_error
      message = "Warning: NoMethodError for #{@dec_class}##{@dec_method}, returned @dec_item instead."
      Logger.new("#{Rails.root}/log/decorator_log.log").warn message
      warn message
    end

    def set_dec_method
      @dec_method = @dec_method || derived_dec_method || :base
    end

    def derived_dec_method
      return unless respond_to? :params      
      return :app if @decorator.respond_to?(:app) && params["isMobile"]
      return :web if @decorator.respond_to?(:web) && params["isWeb"]
    end

    def set_dec_class
      @dec_class = @dec_class ? manual_set_dec_class : derived_dec_class
    end

    def manual_set_dec_class
      "#{@dec_class}Decorator".constantize
    end

    def derived_dec_class
      "#{@dec_item.class.name}Decorator".constantize
    end

    def set_decorator
      @dec_class.new ivars
    end

    def controller_ivars
      Eitil.controller_ivars.map do |ivar|
        { ivar => instance_variable_get("@#{ivar.to_s}") }
      end.inject &:merge
    end

    def ivars
       @dec_kwargs ? controller_ivars.merge(@dec_kwargs) : controller_ivars
    end

  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
eitil-1.2.4 eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb
eitil-1.2.3 eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb
eitil-1.2.2 eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb
eitil-1.2.1 eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb
eitil-1.2.0 eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb
eitil-1.1.39 eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb
eitil-1.1.38 eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb
eitil-1.1.37 eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb
eitil-1.1.36 eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb
eitil-1.1.35 eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb
eitil-1.1.34 eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb
eitil-1.1.33 eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb
eitil-1.1.32 eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb
eitil-1.1.31 eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb
eitil-1.1.30 eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb
eitil-1.1.29 eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb
eitil-1.1.28 eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb
eitil-1.1.27 eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb
eitil-1.1.26 eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb