Sha256: be57291e56922f4fb403d9d62aa3381c895144601358b9916a515d140447f266

Contents?: true

Size: 889 Bytes

Versions: 16

Compression:

Stored size: 889 Bytes

Contents

##
# `ResolveConstant` implements constant resolution. It is implemented as a
# `MethodObject` and therefore returns a result object. By default, constants
# are resolved relative to `Kernal`, but the top-level namespace is
# configurable:
#
#    ShopifyCli::Resolve.call(:object).value # => Object
#    ShopifyCli::Resolve.call('minitest/test').value # => MiniTest::Test
#    ShopifyCli::Resolve.call(:test, namespace: MiniTest) # => MiniTest::Test
#
module ShopifyCli
  class ResolveConstant
    include ShopifyCli::MethodObject
    property! :namespace, accepts: ->(ns) { ns.respond_to?(:const_get) }, default: -> { Kernel }

    def call(name)
      name
        .to_s
        .split(%r{/|:{2}})
        .map { |const_name| const_name.split(/[_-]/).map(&:capitalize).join("") }
        .join("::")
        .yield_self { |const_name| namespace.const_get(const_name) }
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
shopify-cli-2.4.0 lib/shopify-cli/resolve_constant.rb
shopify-cli-2.3.0 lib/shopify-cli/resolve_constant.rb
shopify-cli-2.2.2 lib/shopify-cli/resolve_constant.rb
shopify-cli-2.2.1 lib/shopify-cli/resolve_constant.rb
shopify-cli-2.2.0 lib/shopify-cli/resolve_constant.rb
shopify-cli-2.1.0 lib/shopify-cli/resolve_constant.rb
shopify-cli-2.0.2 lib/shopify-cli/resolve_constant.rb
shopify-cli-2.0.1 lib/shopify-cli/resolve_constant.rb
shopify-cli-2.0.0 lib/shopify-cli/resolve_constant.rb
shopify-cli-1.14.0 lib/shopify-cli/resolve_constant.rb
shopify-cli-1.13.1 lib/shopify-cli/resolve_constant.rb
shopify-cli-1.13.0 lib/shopify-cli/resolve_constant.rb
shopify-cli-1.12.0 lib/shopify-cli/resolve_constant.rb
shopify-cli-1.11.0 lib/shopify-cli/resolve_constant.rb
shopify-cli-1.10.0 lib/shopify-cli/resolve_constant.rb
shopify-cli-1.9.1 lib/shopify-cli/resolve_constant.rb