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) ⇒ Schema

Returns a new instance of Schema.



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

def initialize(name)
  @name = name
  @fields = []
  @validators = []
  @contract_class = nil
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



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

def fields
  @fields
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#validatorsObject (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



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

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

#add_validator(validator_def) ⇒ Object



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

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

#contractObject



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

def contract
  @contract_class ||= build_contract
end

#duplicateObject



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

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

#freezeObject



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

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

#keysObject



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

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

#reset!Object



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

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