Class: Shirinji::Map
- Inherits:
-
Object
- Object
- Shirinji::Map
- Defined in:
- lib/shirinji/map.rb
Instance Attribute Summary collapse
-
#beans ⇒ Object
readonly
Returns the value of attribute beans.
Class Method Summary collapse
-
.load(location) ⇒ Object
Loads a map at a given location.
Instance Method Summary collapse
-
#bean(name, klass: nil, access: :singleton, **others) { ... } ⇒ Object
Add a bean to the map.
-
#get(name) ⇒ Bean
Returns a bean based on its name.
-
#include_map(location) ⇒ Object
Merges another map at a given location.
-
#initialize(&block) ⇒ Map
constructor
A new instance of Map.
-
#merge(map) ⇒ Object
Merges a map into another one.
-
#scope(**options) { ... } ⇒ Object
Scopes a given set of bean to the default options.
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
#beans ⇒ Object (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
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
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) = others.merge( access: access, class_name: klass&.freeze ) beans[name] = Bean.new(name, **, &block) end |
#get(name) ⇒ Bean
Returns a bean based on its name
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
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
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 |