module SearchEnjoy::Schema::ClassMethods

Class methods and variables

Attributes

index_schema[R]

Public Instance Methods

define_json_schema(&block) click to toggle source

define_json_schema do

attr1 :value_type

attr2 :array do
  type :value_type
end

attr3 :hash do
  key :key, :value_type
end

end

# File lib/search_enjoy/schema.rb, line 94
def define_json_schema(&block)
  mapping = Mapping.new

  mapping.instance_eval(&block)

  @index_schema = create_schema(mapping.mapping)
end

Private Instance Methods

create_schema(mapping) click to toggle source
# File lib/search_enjoy/schema.rb, line 124
def create_schema(mapping)
  schema = self
  Dry::Schema.Params do
    mapping.each_pair do |key, value|
      method = schema.send('dry_schema_method', value)

      args = schema.send('dry_schema_args', method, value)

      required(key).send(method, args)
    end
  end
end
dry_schema_args(method, value) click to toggle source

define a arguments for required(key) in Dry::Schema.Params

# File lib/search_enjoy/schema.rb, line 114
def dry_schema_args(method, value)
  if method == :filled
    value
  elsif method == :array
    value[:array].instance_of?(Hash) ? create_schema(value[:array]) : value[:array]
  elsif method == :hash
    create_schema(value)
  end
end
dry_schema_method(value) click to toggle source

define a method for required(key) in Dry::Schema.Params

# File lib/search_enjoy/schema.rb, line 105
def dry_schema_method(value)
  if value.instance_of?(Hash)
    value.key?(:array) ? :array : :hash
  elsif value.instance_of?(Symbol)
    :filled
  end
end