Class: Fluent::Json::Schema::Terms::Obj

Inherits:
Field
  • Object
show all
Includes:
Fluent
Defined in:
lib/fluent/json/schema/terms/obj.rb

Instance Attribute Summary collapse

Attributes inherited from Field

#constraints, #default, #enum, #required, #version

Instance Method Summary collapse

Methods inherited from Field

#mandate, #optionalise, #set

Constructor Details

#initialize(name, options = {}) ⇒ Obj

Returns a new instance of Obj.



13
14
15
16
17
18
# File 'lib/fluent/json/schema/terms/obj.rb', line 13

def initialize(name, options={})
  super(name, options)
  @fields = {}
  @additional ||= false
  @mode = :optional
end

Instance Attribute Details

#additionalObject (readonly)

Returns the value of attribute additional.



11
12
13
# File 'lib/fluent/json/schema/terms/obj.rb', line 11

def additional
  @additional
end

#fieldsObject (readonly)

Returns the value of attribute fields.



11
12
13
# File 'lib/fluent/json/schema/terms/obj.rb', line 11

def fields
  @fields
end

#klassObject (readonly)

Returns the value of attribute klass.



11
12
13
# File 'lib/fluent/json/schema/terms/obj.rb', line 11

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/fluent/json/schema/terms/obj.rb', line 11

def name
  @name
end

Instance Method Details

#[](prop) ⇒ Object



37
38
39
# File 'lib/fluent/json/schema/terms/obj.rb', line 37

def [](prop)
  return @fields[prop]
end

#add(*fields) ⇒ Object



30
31
32
33
34
35
# File 'lib/fluent/json/schema/terms/obj.rb', line 30

def add(*fields)
  fields.each do |field|
    @fields[field.name] = self.prepare(field)
  end
  return self
end

#as_jsonObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fluent/json/schema/terms/obj.rb', line 71

def as_json
  fragment = super.merge!({
    type: :object,
    additionalProperties: @additional,
    required: self.requirements.map { |r| r.name },
    properties: {}
  })

  @fields.map { |k, v| fragment[:properties][k] = v.as_json }

  return fragment
end

#lookup(klass) ⇒ Object



52
53
54
55
56
# File 'lib/fluent/json/schema/terms/obj.rb', line 52

def lookup(klass)
  @name ||= klass.table_name.to_sym
  @klass = klass
  return self
end

#open(allowed = nil) ⇒ Object



41
42
43
44
45
# File 'lib/fluent/json/schema/terms/obj.rb', line 41

def open(allowed=nil)
  @additional = allowed if allowed.present?
  @additional ||= true
  return self
end

#optObject



25
26
27
28
# File 'lib/fluent/json/schema/terms/obj.rb', line 25

def opt
  @mode = :opt 
  return self
end

#reflect(*props) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/fluent/json/schema/terms/obj.rb', line 58

def reflect(*props)
  raise 'No lookup defined' if @klass.nil?
  
  @mode = nil
  props.each { |prop| @fields[prop] = Fluent::Json::Schema::Terms::Reflect.new(prop, @klass) }
  return self
end

#reqObject



20
21
22
23
# File 'lib/fluent/json/schema/terms/obj.rb', line 20

def req
  @mode = :req 
  return self
end

#requirementsObject



66
67
68
69
# File 'lib/fluent/json/schema/terms/obj.rb', line 66

def requirements
  return @fields.map { |k, field| field if field.required }.compact
  return []
end

#strictObject



47
48
49
50
# File 'lib/fluent/json/schema/terms/obj.rb', line 47

def strict
  @additional = false
  return self
end