Class: Apia::Scalar

Inherits:
Object
  • Object
show all
Extended by:
Defineable
Defined in:
lib/apia/scalar.rb

Class Method Summary collapse

Methods included from Defineable

create, inspect, method_missing, name, respond_to_missing?

Class Method Details

.cast(value = nil, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/apia/scalar.rb', line 19

def self.cast(value = nil, &block)
  if block_given? && value.nil?
    return definition.dsl.cast(&block)
  end

  unless valid?(value)
    # Before casting, we'll also validate...
    raise InvalidScalarValueError.new(self, value)
  end

  value = definition.cast.call(value) if definition.cast

  value
end

.definitionApia::Definitions::Object

Return the definition for this type



15
16
17
# File 'lib/apia/scalar.rb', line 15

def self.definition
  @definition ||= Definitions::Scalar.new(Helpers.class_name_to_id(name))
end

.parse(value = nil, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/apia/scalar.rb', line 40

def self.parse(value = nil, &block)
  if block_given? && value.nil?
    return definition.dsl.parse(&block)
  end

  return value if definition.parse.nil?
  return nil if value.nil?

  definition.parse.call(value)
end

.valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/apia/scalar.rb', line 34

def self.valid?(value)
  return true if definition.validator.nil?

  definition.validator.call(value)
end