Class: Expectant::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/expectant/schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, validation: :permissive) ⇒ Schema

Returns a new instance of Schema.



9
10
11
12
13
14
15
# File 'lib/expectant/schema.rb', line 9

def initialize(name, validation: :permissive)
  @name = name
  @fields = []
  @validators = []
  @contract_class = nil
  @validation = validation
end

Instance Attribute Details

#fields ⇒ Object (readonly)

Returns the value of attribute fields.



7
8
9
# File 'lib/expectant/schema.rb', line 7

def fields
  @fields
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/expectant/schema.rb', line 7

def name
  @name
end

#validation ⇒ Object (readonly)

Returns the value of attribute validation.



7
8
9
# File 'lib/expectant/schema.rb', line 7

def validation
  @validation
end

#validators ⇒ Object (readonly)

Returns the value of attribute validators.



7
8
9
# File 'lib/expectant/schema.rb', line 7

def validators
  @validators
end

Instance Method Details

#add_field(expectation) ⇒ Object



32
33
34
35
# File 'lib/expectant/schema.rb', line 32

def add_field(expectation)
  @fields << expectation
  @contract_class = nil
end

#add_validator(validator_def) ⇒ Object



37
38
39
40
# File 'lib/expectant/schema.rb', line 37

def add_validator(validator_def)
  @validators << validator_def
  @contract_class = nil
end

#contract ⇒ Object



21
22
23
# File 'lib/expectant/schema.rb', line 21

def contract
  @contract_class ||= build_contract
end

#duplicate ⇒ Object



25
26
27
28
29
30
# File 'lib/expectant/schema.rb', line 25

def duplicate
  dup = self.class.new(@name, validation: @validation)
  dup.instance_variable_set(:@fields, @fields.dup)
  dup.instance_variable_set(:@validators, @validators.dup)
  dup
end

#freeze ⇒ Object



42
43
44
45
46
# File 'lib/expectant/schema.rb', line 42

def freeze
  @fields.freeze
  @validators.freeze
  super
end

#keys ⇒ Object



17
18
19
# File 'lib/expectant/schema.rb', line 17

def keys
  @fields.map(&:name)
end

#reset! ⇒ Object



48
49
50
51
52
# File 'lib/expectant/schema.rb', line 48

def reset!
  @fields = []
  @validators = []
  @contract_class = nil
end