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(settings = {}, &block) ⇒ Collection

Returns a new instance of Collection.



7
8
9
10
# File 'lib/confo/collection.rb', line 7

def initialize(settings = {}, &block)
  @settings = settings
  configure(&block) if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/confo/collection.rb', line 66

def method_missing(name, *args, &block)
  if args.empty? && name =~ /^(\w+)\?$/
    exists?($1)
  else
    super
  end
end

Instance Attribute Details

#settingsObject (readonly)

Returns the value of attribute settings.



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

def settings
  @settings
end

Instance Method Details

#configure(*args, &block) ⇒ Object



42
43
44
# File 'lib/confo/collection.rb', line 42

def configure(*args, &block)
  args.present? ? define(*args).configure(&block) : instance_eval(&block)
end

#deep_dup(config_settings = {}) ⇒ Object



90
91
92
# File 'lib/confo/collection.rb', line 90

def deep_dup(config_settings = {})
  with_new_settings(config_settings)
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
27
# 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)
    check_config_class!(config_class)
    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.kind_of?(Config)
  config.configure(&block)  if block
  config
end

#dup(config_settings = {}) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/confo/collection.rb', line 82

def dup(config_settings = {})
  self.class.new(settings.merge(config_settings)).tap do |new_obj|
    if storage = @storage
      new_obj.instance_variable_set(:@storage, storage)
    end
  end
end

#each(&block) ⇒ Object



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

def each(&block)
  storage.each { |k, v| block.call(v) }
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/confo/collection.rb', line 46

def exists?(id)
  storage[convert_id(id)].present?
end

#get(id) ⇒ Object Also known as: []



29
30
31
32
33
# File 'lib/confo/collection.rb', line 29

def get(id)
  id = convert_id(id)
  define(id)
  storage[id]
end

#set(id, config) ⇒ Object Also known as: []=



35
36
37
# File 'lib/confo/collection.rb', line 35

def set(id, config)
  storage[convert_id(id)] = config
end

#to_aObject



54
55
56
57
58
# File 'lib/confo/collection.rb', line 54

def to_a
  storage.each_with_object([]) do |(id, instance), memo|
    memo << (instance.respond_to?(:to_hash) ? instance.to_hash : instance)
  end
end

#to_hashObject



60
61
62
63
64
# File 'lib/confo/collection.rb', line 60

def to_hash
  storage.each_with_object({}) do |(id, instance), memo|
    memo[id] = Confo.convert_to_hash(instance)
  end
end

#with_new_settings(new_settings) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/confo/collection.rb', line 74

def with_new_settings(new_settings)
  self.class.new(settings.merge(new_settings)).tap do |new_obj|
    if storage = @storage
      storage.each { |k, v| new_obj.set(k, v.with_new_settings(new_settings) ) }
    end
  end
end