Class: WCC::Contentful::ModelValidators::FieldDsl

Inherits:
Object
  • Object
show all
Defined in:
lib/wcc/contentful/model_validators/dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, field_type, options) ⇒ FieldDsl



69
70
71
72
73
# File 'lib/wcc/contentful/model_validators/dsl.rb', line 69

def initialize(field, field_type, options)
  @field = field.to_s.camelize(:lower) unless field.is_a?(String)
  @type = field_type
  @options = options
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



15
16
17
# File 'lib/wcc/contentful/model_validators/dsl.rb', line 15

def field
  @field
end

Instance Method Details

#schemaObject

“sections”: {

"id": "sections",
"name": "Sections",
"type": "Array",
"localized": false,
"required": false,
"validations": [],
"disabled": false,
"omitted": false,
"items": {
  "type": "Link",
  "validations": [
    {
      "linkContentType": [
        "Section"
      ]
    }
  ],
  "linkType": "Entry"
}

}



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/wcc/contentful/model_validators/dsl.rb', line 39

def schema
  return @field_schema if @field_schema

  # example: required('type').value(...)
  type_pred = parse_type_predicate(@type)

  # example: [required('required').value(eq?: true), ...]
  procs =
    @options.map do |opt|
      if opt.is_a?(Hash)
        opt.map { |k, v| parse_option(k, v) }
      else
        parse_option(opt)
      end
    end

  @field_schema =
    Dry::Validation.Schema do
      instance_eval(&type_pred)

      procs.flatten.each { |p| instance_eval(&p) }
    end
end

#to_procObject



63
64
65
66
67
# File 'lib/wcc/contentful/model_validators/dsl.rb', line 63

def to_proc
  f = field
  s = schema
  proc { required(f).schema(s) }
end