Class: Jschematic::Attributes::Properties

Inherits:
Object
  • Object
show all
Includes:
Composite
Defined in:
lib/jschematic/attributes/properties.rb

Direct Known Subclasses

PatternProperties

Instance Attribute Summary

Attributes included from Element

#parent

Instance Method Summary collapse

Methods included from Composite

#add_child, #children, #each

Methods included from Element

#required?, #schema_for, #title, #to_s

Constructor Details

#initialize(properties) ⇒ Properties

Returns a new instance of Properties.



8
9
10
11
12
13
# File 'lib/jschematic/attributes/properties.rb', line 8

def initialize(properties)
  properties.each_pair do |name, raw_schema|
    schema = Schema.with_name(raw_schema, name)
    add_child(schema)
  end
end

Instance Method Details

#accepts?(instance) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jschematic/attributes/properties.rb', line 15

def accepts?(instance)
  children.all? do |child|
    match = find_instance_for(instance, child.name)

    if match
      value = match.last
    else
      value = nil
    end

    if value.nil? and child.default
      value = child.default
    end

    if value.nil? and child.required?
      fail_validation!("#{child.name} is required", nil)
    elsif !value
      true
    else
      child.accepts?(value) || fail_validation!(child.name, value)
    end
  end
end

#idObject



39
40
41
# File 'lib/jschematic/attributes/properties.rb', line 39

def id
  @parent.id
end