Class: Formally::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/formally/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#finalized_schemaObject (readonly)

Returns the value of attribute finalized_schema.



4
5
6
# File 'lib/formally/config.rb', line 4

def finalized_schema
  @finalized_schema
end

#schemaObject

Returns the value of attribute schema.



3
4
5
# File 'lib/formally/config.rb', line 3

def schema
  @schema
end

Instance Method Details

#finalizeObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/formally/config.rb', line 6

def finalize
  return if @finalized_schema

  _schema     = schema
  _fields     = fields || []
  _predicates = Formally::PredicateFinder.call klass

  @finalized_schema = Dry::Validation.Form base do
    configure do
      _fields.each do |name|
        option name
      end

      option :_self

      _predicates.each do |method|
        if method.arity == 0
          define_method method.name do
            method.bind(_self).call
          end
        else
          define_method method.name do |arg|
            method.bind(_self).call arg
          end
        end
      end

      # class_exec(&_configure) if _configure
    end

    instance_exec(&_schema)
  end

  freeze
end

#new(object) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/formally/config.rb', line 42

def new object
  unless object.is_a? klass
    raise ClassMismatch, "#{object.class} is not a #{@klass}"
  end
  finalize
  State.new self, object
end