Class: Shirinji::Map

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Map

Returns a new instance of Map.



14
15
16
17
18
# File 'lib/shirinji/map.rb', line 14

def initialize(&block)
  @beans = {}

  instance_eval(&block) if block
end

Instance Attribute Details

#beansObject (readonly)

Returns the value of attribute beans.



5
6
7
# File 'lib/shirinji/map.rb', line 5

def beans
  @beans
end

Class Method Details

.load(location) ⇒ Object

Loads a map at a given location

Parameters:

  • location (string)

    path to the map to load



10
11
12
# File 'lib/shirinji/map.rb', line 10

def self.load(location)
  eval(File.read(location))
end

Instance Method Details

#bean(name, klass: nil, access: :singleton, **others) { ... } ⇒ Object

Add a bean to the map

Examples:

build a class bean

map.bean(:foo, klass: 'Foo', access: :singleton)

build a class bean with attributes

map.bean(:foo, klass: 'Foo', access: :singleton) do
  attr :bar, ref: :baz
end

build a value bean

map.bean(:bar, value: 5)

build a lazy evaluated value bean

map.bean(:bar, value: Proc.new { 5 })

Parameters:

  • name (Symbol)

    the name you want to register your bean

  • [String] (Hash)

    a customizable set of options

  • [Object] (Hash)

    a customizable set of options

  • [Boolean] (Hash)

    a customizable set of options

  • [Symbol] (Hash)

    a customizable set of options

Yields:

  • additional method to construct our bean

Raises:

  • (ArgumentError)

    if trying to register a bean that already exist



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/shirinji/map.rb', line 80

def bean(name, klass: nil, access: :singleton, **others, &block)
  name = name.to_sym

  raise_if_name_already_taken!(name)

  options = others.merge(
    access: access,
    class_name: klass&.freeze
  )

  beans[name] = Bean.new(name, **options, &block)
end

#get(name) ⇒ Bean

Returns a bean based on its name

Examples:

accessing a bean

map.get(:foo)
#=> <#Shirinji::Bean ....>

accessing a bean that doesn’t exist

map.get(:bar)
#=> raises ArgumentError (unknown bean)

Parameters:

  • name (Symbol, String)

    the name of the bean you want to access to

Returns:

  • (Bean)

    A bean with the given name or raises an error

Raises:

  • (ArgumentError)

    if trying to access a bean that doesn’t exist



50
51
52
53
54
55
# File 'lib/shirinji/map.rb', line 50

def get(name)
  bean = beans[name.to_sym]
  raise ArgumentError, "Unknown bean #{name}" unless bean

  bean
end

#include_map(location) ⇒ Object

Merges another map at a given location

Parameters:

  • location (string)

    the file to include - must be an absolute path



23
24
25
# File 'lib/shirinji/map.rb', line 23

def include_map(location)
  merge(self.class.load(location))
end

#merge(map) ⇒ Object

Merges a map into another one

Parameters:

Raises:

  • (ArgumentError)

    if both map contains a bean with the same bean



31
32
33
34
35
# File 'lib/shirinji/map.rb', line 31

def merge(map)
  map.beans.keys.each { |name| raise_if_name_already_taken!(name) }

  beans.merge!(map.beans)
end

#scope(**options) { ... } ⇒ Object

Scopes a given set of bean to the default options

It comes pretty handy when used with strongly normative naming

Examples:

module

scope(module: :Foo) do
  bean(:bar, klass: 'Bar')
end

#=> bean(:bar, klass: 'Foo::Bar')

prefix

scope(prefix: :foo) do
  bean(:bar, klass: 'Bar')
end

#=> bean(:foo_bar, klass: 'Bar')

suffix

scope(suffix: :bar) do
  bean(:foo, klass: 'Foo')
end

#=> bean(:foo_bar, klass: 'Foo')

class suffix

scope(klass_suffix: :Bar) do
  bean(:foo, klass: 'Foo')
end

#=> bean(:foo, klass: 'FooBar')

services

scope(module: :Services, klass_suffix: :Service, suffix: :service) do
  scope(module: :User, prefix: :user) do
    bean(:signup, klass: 'Signup')
    bean(:ban, klass: 'Ban')
  end
end

#=> bean(:user_signup_service, klass: 'Services::User::SignupService')
#=> bean(:user_ban_service, klass: 'Services::User::BanService')

Parameters:

  • options (Hash)

Options Hash (**options):

  • :module (Symbol)

    prepend module name to class name

  • :prefix (Symbol)

    prepend prefix to bean name

  • :suffix (Symbol)

    append suffix to bean name

  • :klass_suffix (Symbol)

    append suffix to class name

  • :auto_klass (Boolean)

    generates klass from name

  • :auto_prefix (Boolean)

    generates prefix from module

  • :construct (Boolean)

    applies ‘construct` on every bean

Yields:

  • a standard map



145
146
147
# File 'lib/shirinji/map.rb', line 145

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