Class: Confo::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/confo/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(behaviour_options = {}, &block)
  @behaviour_options = behaviour_options
  configure(&block) if block
end

Instance Attribute Details

#behaviour_optionsObject (readonly)

Returns the value of attribute behaviour_options.



5
6
7
# File 'lib/confo/collection.rb', line 5

def behaviour_options
  @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, options = nil, construct_options = nil, &block)
  id        = convert_id(id)
  config    = storage[id]
  config  ||= begin
    config_class  = Confo.call(self, :config_class, id, construct_options)
    config        = Confo.call(self, :construct_config, config_class, id, construct_options)
    storage[id]   = config
    config.set(:name, id)
    config
  end

  config.set(options)       if options
  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

Returns:

  • (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_aObject



51
52
53
# File 'lib/confo/collection.rb', line 51

def to_a
  storage.values
end

#to_hashObject



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