Class: Alchemy::Configuration::CollectionOption

Inherits:
BaseOption
  • Object
show all
Includes:
Enumerable
Defined in:
lib/alchemy/configuration/collection_option.rb

Instance Attribute Summary collapse

Attributes inherited from BaseOption

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseOption

#==, #allowed_classes, #hash, #raw_value, #validate

Constructor Details

#initialize(value:, name:, item_type:, collection_class: Array, **args) ⇒ CollectionOption

Returns a new instance of CollectionOption.



16
17
18
19
20
21
22
23
24
25
# File 'lib/alchemy/configuration/collection_option.rb', line 16

def initialize(value:, name:, item_type:, collection_class: Array, **args)
  @collection_class = collection_class
  @item_class = get_item_class(item_type)
  @item_args = args
  value = [] if value.nil?
  collection = @collection_class.new(value.map { |value| to_item(value) })
  super(value: collection, name: name)
rescue ConfigurationError => configuration_error
  raise ConfigurationError.new(name, configuration_error.value, configuration_error.allowed_classes)
end

Instance Attribute Details

#collection_classObject (readonly)

Returns the value of attribute collection_class.



14
15
16
# File 'lib/alchemy/configuration/collection_option.rb', line 14

def collection_class
  @collection_class
end

#item_argsObject (readonly)

Returns the value of attribute item_args.



14
15
16
# File 'lib/alchemy/configuration/collection_option.rb', line 14

def item_args
  @item_args
end

#item_classObject (readonly)

Returns the value of attribute item_class.



14
15
16
# File 'lib/alchemy/configuration/collection_option.rb', line 14

def item_class
  @item_class
end

Class Method Details

.value_classObject



10
11
12
# File 'lib/alchemy/configuration/collection_option.rb', line 10

def self.value_class
  Enumerable
end

Instance Method Details

#<<(value) ⇒ Object Also known as: add



31
32
33
# File 'lib/alchemy/configuration/collection_option.rb', line 31

def <<(value)
  @value << to_item(value)
end

#concat(values) ⇒ Object



36
37
38
39
40
# File 'lib/alchemy/configuration/collection_option.rb', line 36

def concat(values)
  values.each do |value|
    add(value)
  end
end

#delete(value) ⇒ Object



42
43
44
# File 'lib/alchemy/configuration/collection_option.rb', line 42

def delete(value)
  @value.delete to_item(value)
end

#each(&block) ⇒ Object



50
51
52
53
54
# File 'lib/alchemy/configuration/collection_option.rb', line 50

def each(&block)
  @value.each do |option|
    yield option.value
  end
end

#to_serializable_arrayObject



56
57
58
59
60
# File 'lib/alchemy/configuration/collection_option.rb', line 56

def to_serializable_array
  to_a.map do |item|
    item.respond_to?(:to_h) ? item.to_h : item
  end
end

#valueObject



27
28
29
# File 'lib/alchemy/configuration/collection_option.rb', line 27

def value
  self
end