Class: Konfig::SchemaGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/konfig/schema_group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchemaGroup

Returns a new instance of SchemaGroup.



11
12
13
# File 'lib/konfig/schema_group.rb', line 11

def initialize
  @attributes = {}
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



9
10
11
# File 'lib/konfig/schema_group.rb', line 9

def attributes
  @attributes
end

Instance Method Details

#add_attribute(name, **kwargs, &block) ⇒ Object



23
24
25
26
# File 'lib/konfig/schema_group.rb', line 23

def add_attribute(name, **kwargs, &block)
  kwargs[:transformer] = block if block && !kwargs.key?(:transformer)
  @attributes[name] = SchemaAttribute.new(name, **kwargs)
end

#attribute(name) ⇒ Object



15
16
17
# File 'lib/konfig/schema_group.rb', line 15

def attribute(name)
  @attributes[name] || raise(AttributeNotFoundError, "Attribute '#{name}' not found in schema")
end

#attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/konfig/schema_group.rb', line 19

def attribute?(name)
  @attributes.key?(name)
end

#create_hash(path, source = nil) ⇒ Object

Create a hash of the all the values for this group from the given source.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/konfig/schema_group.rb', line 29

def create_hash(path, source = nil)
  @attributes.each_with_object({}) do |(name, attribute), hash|
    attribute_path = path + [name]
    if source.nil?
      hash[name] = attribute.transform(attribute.default)
    else
      begin
        source_value = source.get(attribute_path, attribute: attribute)
        hash[name] = attribute.transform(source_value)
      rescue ValueNotPresentError
        # This is OK
      end
    end
  end
end