Class: Confo::Collection
- Inherits:
-
Object
- Object
- Confo::Collection
- Includes:
- Enumerable
- Defined in:
- lib/confo/collection.rb
Instance Attribute Summary collapse
-
#behaviour_options ⇒ Object
readonly
Returns the value of attribute behaviour_options.
Instance Method Summary collapse
- #configure(*args, &block) ⇒ Object
- #define(id, options = nil, construct_options = nil, &block) ⇒ Object
- #each(&block) ⇒ Object
- #exists?(id) ⇒ Boolean
- #get(id) ⇒ Object (also: #[])
-
#initialize(behaviour_options = {}, &block) ⇒ Collection
constructor
A new instance of Collection.
- #set(id, config) ⇒ Object (also: #[]=)
- #to_a ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(behaviour_options = {}, &block) ⇒ Collection
Returns a new instance of Collection.
7 8 9 10 |
# File 'lib/confo/collection.rb', line 7 def initialize( = {}, &block) @behaviour_options = configure(&block) if block end |
Instance Attribute Details
#behaviour_options ⇒ Object (readonly)
Returns the value of attribute behaviour_options.
5 6 7 |
# File 'lib/confo/collection.rb', line 5 def @behaviour_options end |
Instance Method Details
#configure(*args, &block) ⇒ Object
39 40 41 |
# File 'lib/confo/collection.rb', line 39 def configure(*args, &block) args.present? ? define(*args).configure(&block) : instance_eval(&block) end |
#define(id, options = nil, construct_options = nil, &block) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/confo/collection.rb', line 12 def define(id, = nil, = nil, &block) id = convert_id(id) config = storage[id] config ||= begin config_class = Confo.call(self, :config_class, id, ) config = Confo.call(self, :construct_config, config_class, id, ) storage[id] = config config.set(:name, id) config end config.set() if config.configure(&block) if block config end |
#each(&block) ⇒ Object
47 48 49 |
# File 'lib/confo/collection.rb', line 47 def each(&block) storage.each { |k, v| block.call(v) } end |
#exists?(id) ⇒ Boolean
43 44 45 |
# File 'lib/confo/collection.rb', line 43 def exists?(id) storage[convert_id(id)].present? end |
#get(id) ⇒ Object Also known as: []
28 29 30 |
# File 'lib/confo/collection.rb', line 28 def get(id) define(id) end |
#set(id, config) ⇒ Object Also known as: []=
32 33 34 |
# File 'lib/confo/collection.rb', line 32 def set(id, config) storage[convert_id(id)] = config end |
#to_a ⇒ Object
51 52 53 |
# File 'lib/confo/collection.rb', line 51 def to_a storage.values end |
#to_hash ⇒ Object
55 56 57 58 59 60 |
# File 'lib/confo/collection.rb', line 55 def to_hash storage.reduce({}) do |memo, (id, instance)| memo[id] = Confo.convert_to_hash(instance) memo end end |