class SknRegistry

## SknRegistry

Credits: Inspired by: andyholland1991@aol.com, cv.droppages.com

Syntax:

 reg = SknRegistry.new(&reg_blk)
       - where: &reg_blk is {|reg| reg.register(...); ... }

 self_chain = reg.register(key, content, options={}, &block)
    content = reg.resolve(key, render_proc=true)

Params:
       key    - anyThing can be used as the key
   content    - anyThing can be used as value; Class, Proc, instance, value
   options    - hash of dependencies to pass into procs when rendering
    &block    - block used for #content; with/without a parameter. ex: {|parm| ...} | { ... }

render_proc - bool: when content is_a Proc, should it be call()'ed before being returned

## Examples

class Person
  attr_reader :first, :last
  def initialize(names={})
    self.first = names[:first]
    self.last = names[:last]
  end
  def name
    "#{first}.#{last}"
  end
end

##

Using Classes: default no #call before return

##

reg.register(:user, Person)
-- or --
reg.register(:user, Person, call: false )
-- then --
reg.resolve(:user).new({first: 'Monty', last: 'Python'}).name        # => 'Monty.Python'
reg.resolve(:user).new({first: 'Monty', last: 'Python'}).name        # => 'Monty.Python'

##

Using Procs: default #call before return

##

reg.register(:user, -> { Person.new })
-- or --
reg.register(:user, -> { Person.new }, call: false )
-- or --
reg.register(:user, ->(hsh) { Person.new(hsh) }, call: false )
-- or --
reg.register(:block_a, ->(hsh) { Person.new(hsh) }, {call: true, first: 'Monty', last: 'Python'} )
-- or --
reg.register(:block_b, {call: true, greet: 'Hello', str: 'Python'}) {|hsh| "#{hsh[:greet]} #{hsh[:str].upcase}" }
-- then --
reg.resolve(:person_repository).name         # => '.'
reg.resolve(:person_repository).call().name  # => '.'
reg.resolve(:person_repository).call({first: 'Monty', last: 'Python'}).name  # => 'Monty.Python'
reg.resolve(:block_a).name                   # => 'Monty.Python'
reg.resolve(:block_b)                        # => 'Hello PYTHON'

Public Class Methods

new(&block) click to toggle source

SknRegistry initializer

Calls superclass method
# File lib/skn_registry.rb, line 95
def initialize(&block)
  super
  block.call(self) if block_given?
  @__substitutes = {}
end

Public Instance Methods

register(key, contents = nil, options = {}, &block) click to toggle source

public instance methods

# File lib/skn_registry.rb, line 102
def register(key, contents = nil, options = {}, &block)
  if block_given?
    item = block
    options.merge!(contents) if contents.is_a?(::Hash)
  else
    item = contents
  end

  self.store( key, Content.new(item, options) )

  self # enable chaining
end
register_mock(key, contents = nil, options = {}, &block) click to toggle source
# File lib/skn_registry.rb, line 119
def register_mock(key, contents = nil, options = {}, &block)
  if member?(key)
    @__substitutes.store(key, self.delete(key) )
  end

  register(key, contents, options, &block)
end
Also aliased as: substitute
resolve(key, render_proc=true) click to toggle source
# File lib/skn_registry.rb, line 115
def resolve(key, render_proc=true)  # false to prevent downstream #call
  self[key]&.call(render_proc)
end
restore!()
Alias for: unregister_mocks!
substitute(key, contents = nil, options = {}, &block)
Alias for: register_mock
unregister_mocks!() click to toggle source
# File lib/skn_registry.rb, line 128
def unregister_mocks!
  @__substitutes.keys.each do |k|
    self[k] = @__substitutes.delete(k)
  end

  nil
end
Also aliased as: restore!