Class: Natsuzora::Contract::AST::Scalar

Inherits:
Node
  • Object
show all
Defined in:
lib/natsuzora/contract/ast/scalar.rb

Overview

Scalar value with type and modifier.

Defined Under Namespace

Modules: Modifier

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scalar_type, modifier = Modifier::NONE) ⇒ Scalar

Returns a new instance of Scalar.



23
24
25
26
27
# File 'lib/natsuzora/contract/ast/scalar.rb', line 23

def initialize(scalar_type, modifier = Modifier::NONE)
  super()
  @scalar_type = scalar_type
  @modifier = modifier
end

Instance Attribute Details

#modifierObject (readonly)

Returns the value of attribute modifier.



21
22
23
# File 'lib/natsuzora/contract/ast/scalar.rb', line 21

def modifier
  @modifier
end

#scalar_typeObject (readonly)

Returns the value of attribute scalar_type.



21
22
23
# File 'lib/natsuzora/contract/ast/scalar.rb', line 21

def scalar_type
  @scalar_type
end

Instance Method Details

#==(other) ⇒ Object



52
53
54
55
56
# File 'lib/natsuzora/contract/ast/scalar.rb', line 52

def ==(other)
  other.is_a?(Scalar) &&
    other.scalar_type == @scalar_type &&
    other.modifier == @modifier
end

#accepts?(data) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
# File 'lib/natsuzora/contract/ast/scalar.rb', line 37

def accepts?(data)
  case @scalar_type
  when ScalarType::STRING then data.is_a?(String)
  when ScalarType::INTEGER then data.is_a?(Integer)
  when ScalarType::BOOL then data.is_a?(TrueClass) || data.is_a?(FalseClass)
  when ScalarType::SCALAR then data.is_a?(String) || data.is_a?(Integer)
  end
end

#nullable?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/natsuzora/contract/ast/scalar.rb', line 29

def nullable?
  @modifier == Modifier::NULLABLE
end

#required?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/natsuzora/contract/ast/scalar.rb', line 33

def required?
  @modifier == Modifier::REQUIRED
end

#to_hObject



46
47
48
49
50
# File 'lib/natsuzora/contract/ast/scalar.rb', line 46

def to_h
  h = { 'kind' => 'scalar', 'type' => @scalar_type.to_s }
  h['modifier'] = @modifier.to_s unless @modifier == Modifier::NONE
  h
end