Sha256: 4b7f95a88f40f3abb0c60fe1bd346cdf19539949cb18cdb1b705704bc5c880a4

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module Shirinji
  class Scope
    VALID_OPTIONS = %i[module prefix suffix klass_suffix].freeze

    attr_reader :parent, :mod, :prefix, :suffix, :klass_suffix

    def initialize(parent, **options, &block)
      validate_options(options)

      @parent = parent
      @mod = options[:module]
      @prefix = options[:prefix]
      @suffix = options[:suffix]
      @klass_suffix = options[:klass_suffix]

      instance_eval(&block) if block
    end

    def bean(name, klass: nil, **others, &block)
      chunks = [mod, "#{klass}#{klass_suffix}"].compact
      options = others.merge(klass: klass ? chunks.join('::') : nil)
      scoped_name = [prefix, name, suffix].compact.join('_')

      parent.bean(scoped_name, **options, &block)
    end

    def scope(**options, &block)
      Scope.new(self, **options, &block)
    end

    private

    def validate_options(args)
      args.each_key do |k|
        next if Shirinji::Scope::VALID_OPTIONS.include?(k)
        raise ArgumentError, "Unknown key #{k}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shirinji-0.0.3 lib/shirinji/scope.rb