Class: Leftovers::ConfigLoader::ObjectSchema

Inherits:
Schema
  • Object
show all
Defined in:
lib/leftovers/config_loader/object_schema.rb

Class Method Summary collapse

Methods inherited from Schema

===, error

Class Method Details

.attribute(name, value_schema, aliases: nil, require_group: nil, suggest: true) ⇒ Object



17
18
19
20
21
22
# File 'lib/leftovers/config_loader/object_schema.rb', line 17

def attribute(name, value_schema, aliases: nil, require_group: nil, suggest: true)
  attributes_and_schemas_to_inherit_from << Attribute.new(
    name, value_schema,
    aliases: aliases, require_group: require_group, suggest: suggest
  )
end

.attributesObject



13
14
15
# File 'lib/leftovers/config_loader/object_schema.rb', line 13

def attributes
  attributes_and_schemas_to_inherit_from.flat_map(&:attributes)
end

.inherit_attributes_from(schema, require_group: true, except: nil) ⇒ Object



7
8
9
10
11
# File 'lib/leftovers/config_loader/object_schema.rb', line 7

def inherit_attributes_from(schema, require_group: true, except: nil)
  attributes_and_schemas_to_inherit_from << InheritSchemaAttributes.new(
    schema, require_group: require_group, except: except
  )
end

.require_groupsObject



24
25
26
# File 'lib/leftovers/config_loader/object_schema.rb', line 24

def require_groups
  attributes.group_by(&:require_group).tap { |rg| rg.delete(nil) }.values
end

.to_ruby(node) ⇒ Object



38
39
40
# File 'lib/leftovers/config_loader/object_schema.rb', line 38

def to_ruby(node)
  node.pairs.map { |(key, value)| attribute_for_key(key).to_ruby(value) }.to_h
end

.validate(node) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/leftovers/config_loader/object_schema.rb', line 28

def validate(node)
  if node.hash?
    validate_attributes(node)
  else
    error(node, 'be a hash')

    node.valid?
  end
end