Module: Expectant::DSL::ClassMethods

Defined in:
lib/expectant/dsl.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



14
15
16
17
18
# File 'lib/expectant/dsl.rb', line 14

def self.extended(base)
  base.class_eval do
    @_expectant_schemas = {}
  end
end

Instance Method Details

#expects(schema_name, collision: :error, singular: nil) ⇒ Object

Define a new expectation schema Options:

collision: :error|:force -> method name collision policy for dynamic definitions
singular: string|symbol  -> control singular name for the schema


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/expectant/dsl.rb', line 35

def expects(schema_name, collision: :error, singular: nil)
  field_method_name = Utils.singularize(schema_name.to_sym)
  if !singular.nil?
    if singular.is_a?(String) || singular.is_a?(Symbol)
      field_method_name = singular.to_sym
    else
      raise ConfigurationError, "Invalid singular option: #{singular.inspect}"
    end
  end
  schema = schema_name.to_sym

  if @_expectant_schemas.key?(schema)
    raise SchemaError, "Schema :#{schema} already defined"
  else
    create_schema(schema, collision: collision, field_method_name: field_method_name)
  end

  self
end

#inherited(sub) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/expectant/dsl.rb', line 20

def inherited(sub)
  return unless instance_variable_defined?(:@_expectant_schemas)
  return if @_expectant_schemas.empty?

  # Deep copy each schema (fields + validators)
  parent_schemas = @_expectant_schemas
  duped = parent_schemas.transform_values { |schema| schema.duplicate }

  sub.instance_variable_set(:@_expectant_schemas, duped)
end

#reset_inherited_expectations!Object



55
56
57
# File 'lib/expectant/dsl.rb', line 55

def reset_inherited_expectations!
  @_expectant_schemas = {}
end