Class: GoldenFleece::Schema

Inherits:
Object
  • Object
show all
Includes:
Utility
Defined in:
lib/golden_fleece/schema.rb

Constant Summary

Constants included from Utility

Utility::FALSE_VALUES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utility

#build_json_path, #cast_boolean, #deep_stringify_keys, #error_suffix

Constructor Details

#initialize(context, path, definitions) ⇒ Schema

Returns a new instance of Schema.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/golden_fleece/schema.rb', line 11

def initialize(context, path, definitions)
  @context = context
  @path = path
  @name = path.split("/").last
  @attribute = path.split("/")[1]
  @json_path = path.split("/")[2..-1]
  @json_path = @json_path.join("/") if @json_path
  @subschemas = {}

  # .count == 1 means we're at the root
  # .count == 2 means we're at the attribute
  # .count >= 3 means we're cookin'
  if path.split("/").count <= 2
    @types = [Definitions::TYPES[:object]]
    map_subschemas(definitions)
  else
    map_value
    map_types(definitions[:type], definitions[:types])
    map_normalizers(definitions[:normalizer], definitions[:normalizers])
    map_format(definitions[:format])
    map_default(definitions[:default])
    map_subschemas(definitions[:subschemas])
  end
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



9
10
11
# File 'lib/golden_fleece/schema.rb', line 9

def attribute
  @attribute
end

#defaultObject (readonly)

Returns the value of attribute default.



9
10
11
# File 'lib/golden_fleece/schema.rb', line 9

def default
  @default
end

#formatObject (readonly)

Returns the value of attribute format.



9
10
11
# File 'lib/golden_fleece/schema.rb', line 9

def format
  @format
end

#json_pathObject (readonly)

Returns the value of attribute json_path.



9
10
11
# File 'lib/golden_fleece/schema.rb', line 9

def json_path
  @json_path
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/golden_fleece/schema.rb', line 9

def name
  @name
end

#normalizersObject (readonly)

Returns the value of attribute normalizers.



9
10
11
# File 'lib/golden_fleece/schema.rb', line 9

def normalizers
  @normalizers
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/golden_fleece/schema.rb', line 9

def path
  @path
end

#typesObject (readonly)

Returns the value of attribute types.



9
10
11
# File 'lib/golden_fleece/schema.rb', line 9

def types
  @types
end

#valueObject (readonly)

Returns the value of attribute value.



9
10
11
# File 'lib/golden_fleece/schema.rb', line 9

def value
  @value
end

Instance Method Details

#[](subschema_name) ⇒ Object



36
37
38
# File 'lib/golden_fleece/schema.rb', line 36

def [](subschema_name)
  subschemas[subschema_name]
end

#[]=(subschema_name, subschema_definition) ⇒ Object



40
41
42
# File 'lib/golden_fleece/schema.rb', line 40

def []=(subschema_name, subschema_definition)
  subschemas[subschema_name] = Schema.new(context, build_json_path(path, subschema_name), subschema_definition)
end

#each(&block) ⇒ Object



44
45
46
# File 'lib/golden_fleece/schema.rb', line 44

def each(&block)
  subschemas.each(&block)
end

#keysObject



56
57
58
# File 'lib/golden_fleece/schema.rb', line 56

def keys
  subschemas.keys
end

#parent?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/golden_fleece/schema.rb', line 52

def parent?
  subschemas.count > 0
end

#reduce(memo, &block) ⇒ Object



48
49
50
# File 'lib/golden_fleece/schema.rb', line 48

def reduce(memo, &block)
  subschemas.reduce(memo, &block)
end

#valuesObject



60
61
62
# File 'lib/golden_fleece/schema.rb', line 60

def values
  subschemas.values
end