Class: Shirinji::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/shirinji/scope.rb

Constant Summary collapse

VALID_OPTIONS =
%i[
  module prefix suffix klass_suffix auto_klass auto_prefix construct
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, **options, &block) ⇒ Scope

Returns a new instance of Scope.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/shirinji/scope.rb', line 12

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

  @parent = parent
  @mod = options[:module]
  @suffix = options[:suffix]
  @klass_suffix = options[:klass_suffix]
  @auto_klass = options[:auto_klass]
  @auto_prefix = options[:auto_prefix]
  @prefix = generate_prefix(options[:prefix])
  @construct = options.fetch(:construct, true)

  instance_eval(&block) if block
end

Instance Attribute Details

#auto_klassObject (readonly)

Returns the value of attribute auto_klass.



9
10
11
# File 'lib/shirinji/scope.rb', line 9

def auto_klass
  @auto_klass
end

#auto_prefixObject (readonly)

Returns the value of attribute auto_prefix.



9
10
11
# File 'lib/shirinji/scope.rb', line 9

def auto_prefix
  @auto_prefix
end

#constructObject (readonly)

Returns the value of attribute construct.



9
10
11
# File 'lib/shirinji/scope.rb', line 9

def construct
  @construct
end

#klass_suffixObject (readonly)

Returns the value of attribute klass_suffix.



9
10
11
# File 'lib/shirinji/scope.rb', line 9

def klass_suffix
  @klass_suffix
end

#modObject (readonly)

Returns the value of attribute mod.



9
10
11
# File 'lib/shirinji/scope.rb', line 9

def mod
  @mod
end

#parentObject (readonly)

Returns the value of attribute parent.



9
10
11
# File 'lib/shirinji/scope.rb', line 9

def parent
  @parent
end

#prefixObject (readonly)

Returns the value of attribute prefix.



9
10
11
# File 'lib/shirinji/scope.rb', line 9

def prefix
  @prefix
end

#suffixObject (readonly)

Returns the value of attribute suffix.



9
10
11
# File 'lib/shirinji/scope.rb', line 9

def suffix
  @suffix
end

Instance Method Details

#bean(name, klass: nil, **options, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/shirinji/scope.rb', line 27

def bean(name, klass: nil, **options, &block)
  default_opts = compact({ construct: construct })

  klass = generate_klass(name, klass) unless options[:value]
  options = compact(default_opts.merge(options).merge(klass: klass))
  scoped_name = generate_scope(name)

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

#scope(**options, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/shirinji/scope.rb', line 37

def scope(**options, &block)
  opts = {
    auto_klass: auto_klass,
    auto_prefix: auto_prefix,
    construct: construct
  }.merge(options)

  Scope.new(self, **opts, &block)
end