Sha256: 170f4d16be1c0288320898a4f1f344fed1eec07957c442403a1a3558a2e546eb

Contents?: true

Size: 870 Bytes

Versions: 23

Compression:

Stored size: 870 Bytes

Contents

module Eco
  module API
    module Common
      module ClassHelpers

        def class_resolver(name, klass)
          define_singleton_method(name) { resolve_class(klass) }
          define_method(name) { self.class.resolve_class(klass) }
        end

        def resolve_class(klass, exception: true)
          @resolved ||= {}
          @resolved[klass] ||=
            case klass
              when Class
                klass
              when String
                begin
                  Kernel.const_get(klass)
                rescue NameError => e
                  raise e if exception
                end
              when Symbol
                resolve_class(self.send(klass))
              else
                raise "Unknown class: #{klass}" if exception
            end
        end
        
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
eco-helpers-1.1.7 lib/eco/api/common/class_helpers.rb
eco-helpers-1.1.6 lib/eco/api/common/class_helpers.rb
eco-helpers-1.1.5 lib/eco/api/common/class_helpers.rb
eco-helpers-1.1.4 lib/eco/api/common/class_helpers.rb
eco-helpers-1.1.3 lib/eco/api/common/class_helpers.rb
eco-helpers-1.1.2 lib/eco/api/common/class_helpers.rb
eco-helpers-1.1.1 lib/eco/api/common/class_helpers.rb
eco-helpers-1.0.14 lib/eco/api/common/class_helpers.rb
eco-helpers-1.0.13 lib/eco/api/common/class_helpers.rb
eco-helpers-1.0.12 lib/eco/api/common/class_helpers.rb
eco-helpers-1.0.11 lib/eco/api/common/class_helpers.rb
eco-helpers-1.0.10 lib/eco/api/common/class_helpers.rb
eco-helpers-1.0.9 lib/eco/api/common/class_helpers.rb
eco-helpers-1.0.8 lib/eco/api/common/class_helpers.rb
eco-helpers-1.0.7 lib/eco/api/common/class_helpers.rb
eco-helpers-1.0.6 lib/eco/api/common/class_helpers.rb
eco-helpers-1.0.5 lib/eco/api/common/class_helpers.rb
eco-helpers-1.0.4 lib/eco/api/common/class_helpers.rb
eco-helpers-1.0.3 lib/eco/api/common/class_helpers.rb
eco-helpers-0.9.5 lib/eco/api/common/class_helpers.rb