Class: Jsapi::Meta::Existence
- Inherits:
-
Object
- Object
- Jsapi::Meta::Existence
- Includes:
- Comparable
- Defined in:
- lib/jsapi/meta/existence.rb
Overview
Combines the presence concepts of the #present? method and JSON Schema by four levels of existence.
Constant Summary collapse
- ALLOW_OMITTED =
The parameter or property can be omitted, corresponds to the opposite of
requiredas specified by JSON Schema. new(1)
- ALLOW_NIL =
The parameter or property value can be
nil, corresponds tonullable: trueas specified by JSON Schema. new(2)
- ALLOW_EMPTY =
The parameter or property value must respond to
nil?withfalse. new(3)
- PRESENT =
The parameter or property value must respond to
present?withtrueor must befalse. new(4)
Instance Attribute Summary collapse
-
#level ⇒ Object
readonly
The level of existence.
Class Method Summary collapse
-
.from(value) ⇒ Object
Transforms
valueto an instance of this class.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
:nodoc:.
-
#==(other) ⇒ Object
:nodoc:.
-
#initialize(level) ⇒ Existence
constructor
Creates a new instance with the specified level.
-
#inspect ⇒ Object
:nodoc:.
-
#reach?(object) ⇒ Boolean
Returns true if
objectreaches the level of existence, false otherwise.
Constructor Details
#initialize(level) ⇒ Existence
Creates a new instance with the specified level.
14 15 16 |
# File 'lib/jsapi/meta/existence.rb', line 14 def initialize(level) @level = level end |
Instance Attribute Details
#level ⇒ Object (readonly)
The level of existence.
11 12 13 |
# File 'lib/jsapi/meta/existence.rb', line 11 def level @level end |
Class Method Details
.from(value) ⇒ Object
Transforms value to an instance of this class.
Raises an ArgumentError if value could not be transformed.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/jsapi/meta/existence.rb', line 36 def self.from(value) return value if value.is_a?(Existence) case value when :present, true PRESENT when :allow_empty ALLOW_EMPTY when :allow_nil, :allow_null ALLOW_NIL when :allow_omitted, false, nil ALLOW_OMITTED else raise ArgumentError, "invalid existence: #{value}" end end |
Instance Method Details
#<=>(other) ⇒ Object
:nodoc:
57 58 59 |
# File 'lib/jsapi/meta/existence.rb', line 57 def <=>(other) # :nodoc: level <=> other.level end |
#==(other) ⇒ Object
:nodoc:
53 54 55 |
# File 'lib/jsapi/meta/existence.rb', line 53 def ==(other) # :nodoc: other.is_a?(Existence) && level == other.level end |
#inspect ⇒ Object
:nodoc:
61 62 63 |
# File 'lib/jsapi/meta/existence.rb', line 61 def inspect # :nodoc: "#<#{self.class.name} level: #{level}>" end |
#reach?(object) ⇒ Boolean
Returns true if object reaches the level of existence, false otherwise.
66 67 68 |
# File 'lib/jsapi/meta/existence.rb', line 66 def reach?(object) (object.null? ? 2 : object.empty? ? 3 : 4) >= level end |