Sha256: 349b0aa11bdfbfa7b90975859eeb16d819e6cd9cbd5cb951578699fc58594a96

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

module Xpose
  class Decorated
    attr_accessor :conf

    def initialize(**args)
      @conf = ::Xpose::Configuration.build(args)
      raise MissingParameter if conf.name.nil?
    end

    def call(instance)
      v = instance.send(conf.method_name)
      if conf.decorator == :infer
        infer(v)
      elsif Class === conf.decorator
        decorator.new(v)
      elsif decorator.respond_to?(:call)
        decorator.call(v)
      elsif Symbol === decorator
        decorator.to_s.singularize.capitalize.constantize.new(v)
      else
        raise StandardError.new('Unknown decorator')
      end
    end

    private

    def infer(v)
      if v.respond_to?(:decorate)
        v.decorate
      elsif class_exists?(klass)
        klass.new(v)
      else
        raise UnknownDecoratorError
      end
    end

    def klass
      @klass ||= "#{conf.singularized_name.capitalize}Decorator".constantize
    end

    def class_exists?(class_name)
      Module.const_get(class_name).is_a?(Class)
    rescue NameError
      return false
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
xpose-0.1.2 lib/xpose/decorated.rb
xpose-0.1.1 lib/xpose/decorated.rb
xpose-0.1.0 lib/xpose/decorated.rb