Class: Wardrobe::Stores
- Inherits:
-
Object
- Object
- Wardrobe::Stores
- Defined in:
- lib/wardrobe/stores.rb
Instance Attribute Summary collapse
-
#stores ⇒ Object
readonly
Returns the value of attribute stores.
Class Method Summary collapse
Instance Method Summary collapse
- #add_attribute(name, klass, defining_object, **merged_args, &blk) ⇒ Object
- #add_store(name, klass, initializer: false) ⇒ Object
- #dup ⇒ Object
- #enable_plugin(name, **args) ⇒ Object
-
#initialize ⇒ Stores
constructor
A new instance of Stores.
- #merge(other, calling_object) ⇒ Object
- #remove_attribute(name) ⇒ Object
- #update(&blk) ⇒ Object
Constructor Details
#initialize ⇒ Stores
Returns a new instance of Stores.
19 20 21 22 23 24 25 |
# File 'lib/wardrobe/stores.rb', line 19 def initialize @stores = {}.freeze self.class.registered_stores.each do |key, value| add_store(key, value, initializer: true) end freeze end |
Instance Attribute Details
#stores ⇒ Object (readonly)
Returns the value of attribute stores.
17 18 19 |
# File 'lib/wardrobe/stores.rb', line 17 def stores @stores end |
Class Method Details
.register_store(name, klass) ⇒ Object
9 10 11 |
# File 'lib/wardrobe/stores.rb', line 9 def self.register_store(name, klass) @registered_stores = registered_stores.merge(name => klass).freeze end |
.registered_stores ⇒ Object
5 6 7 |
# File 'lib/wardrobe/stores.rb', line 5 def self.registered_stores @registered_stores ||= {}.freeze end |
Instance Method Details
#add_attribute(name, klass, defining_object, **merged_args, &blk) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/wardrobe/stores.rb', line 86 def add_attribute(name, klass, defining_object, **merged_args, &blk) if frozen? dup.add_attribute(name, klass, defining_object, **merged_args, &blk) else @attribute_store = attribute_store.add( name, klass, defining_object, self, **merged_args, &blk ) freeze end end |
#add_store(name, klass, initializer: false) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/wardrobe/stores.rb', line 36 def add_store(name, klass, initializer: false) if frozen? dup.add_store(name, klass) else @stores = stores.merge(name => klass) instance_variable_set("@#{name}", klass.new) define_singleton_method(name) { instance_variable_get("@#{name}") } freeze unless initializer end end |
#dup ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/wardrobe/stores.rb', line 64 def dup duplicate = super duplicate.stores.each do |name, _klass| duplicate.define_singleton_method(name) do instance_variable_get("@#{name}") end end duplicate end |
#enable_plugin(name, **args) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/wardrobe/stores.rb', line 74 def enable_plugin(name, **args) if frozen? dup.enable_plugin(name, **args) else @plugin_store = plugin_store.add(name, **args) plugin_store[name][:klass]..each do |option| @option_store = option_store.add(option.name, option) end freeze end end |
#merge(other, calling_object) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/wardrobe/stores.rb', line 47 def merge(other, calling_object) if frozen? dup.merge(other, calling_object) else (other.stores.keys - stores.keys).each do |name| add_store(name, other.stores[name], initializer: true) end stores.each do |name, klass| instance = respond_to?(name) ? send(name) : klass.new instance_variable_set( "@#{name}", instance.merge(other.send(name), calling_object, self) ) end freeze end end |
#remove_attribute(name) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/wardrobe/stores.rb', line 97 def remove_attribute(name) if frozen? dup.remove_attribute(name) else @attribute_store = attribute_store.del(name) freeze end end |
#update(&blk) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/wardrobe/stores.rb', line 27 def update(&blk) if frozen? dup.update(&blk) else instance_exec(&blk) freeze end end |