Class: Jschematic::Attributes::Required

Inherits:
Object
  • Object
show all
Includes:
Element
Defined in:
lib/jschematic/attributes/required.rb

Instance Attribute Summary

Attributes included from Element

#id, #parent

Instance Method Summary collapse

Methods included from Element

#schema_for, #title, #to_s

Constructor Details

#initialize(required = false) ⇒ Required

Returns a new instance of Required.



8
9
10
11
12
13
14
15
# File 'lib/jschematic/attributes/required.rb', line 8

def initialize(required=false)
  case required
  when TrueClass, FalseClass
    @required = required
  else
    raise "Require must be strictly true or false. Truthy and false values are not allowed."
  end
end

Instance Method Details

#accepts?(instance) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
# File 'lib/jschematic/attributes/required.rb', line 21

def accepts?(instance)
  if @required
    instance || fail_validation!(@required, instance)
  else
    true
  end
end

#required?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/jschematic/attributes/required.rb', line 17

def required?
  @required
end