Class: Finitio::BuiltinType

Inherits:
Type
  • Object
show all
Defined in:
lib/finitio/type/builtin_type.rb,
lib/finitio/generation/builtin_type.rb,
lib/finitio/json_schema/builtin_type.rb

Overview

A BuiltinType generator allows capuring an information type to a type of the host language, here a Ruby class. For instance,

Int := BuiltinType(ruby.Integer)

The set of values captured by the information type is the same set of values that can be represented by the host type. In the example, ‘Int` captures the same set of numbers as ruby’s Integer.

The ruby class is used as concrete representation of the information type. In the example:

R(Int) = Integer

Accordingly, the ‘dress` transformation function has the following signature:

dress :: Alpha  -> Int    throws TypeError
dress :: Object -> Integer throws TypeError

Constant Summary

Constants included from Metadata

Metadata::EMPTY_METADATA

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#anonymous?, #name, #name=, #named?, #suppremum, #to_s, #unconstrained

Methods included from Metadata

#metadata, #metadata=, #metadata?

Constructor Details

#initialize(ruby_type, name = nil, metadata = nil) ⇒ BuiltinType

Returns a new instance of BuiltinType.



24
25
26
27
# File 'lib/finitio/type/builtin_type.rb', line 24

def initialize(ruby_type, name = nil,  = nil)
  super(name, )
  @ruby_type = ruby_type
end

Instance Attribute Details

#ruby_typeObject (readonly) Also known as: representator

Returns the value of attribute ruby_type.



28
29
30
# File 'lib/finitio/type/builtin_type.rb', line 28

def ruby_type
  @ruby_type
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



47
48
49
# File 'lib/finitio/type/builtin_type.rb', line 47

def ==(other)
  super || (other.is_a?(BuiltinType) && other.ruby_type==ruby_type)
end

#default_nameObject



32
33
34
# File 'lib/finitio/type/builtin_type.rb', line 32

def default_name
  @ruby_type.name.to_s
end

#dress(value, handler = DressHelper.new) ⇒ Object

Check that ‘value` is a valid instance of `ruby_type` through `===` or raise an error.



42
43
44
45
# File 'lib/finitio/type/builtin_type.rb', line 42

def dress(value, handler = DressHelper.new)
  handler.failed!(self, value) unless ruby_type===value
  value
end

#generate_data(generator, world = nil) ⇒ Object



4
5
6
# File 'lib/finitio/generation/builtin_type.rb', line 4

def generate_data(generator, world = nil)
  generator.heuristic.call(ruby_type, generator, world)
end

#hashObject



52
53
54
# File 'lib/finitio/type/builtin_type.rb', line 52

def hash
  self.class.hash ^ ruby_type.hash
end

#include?(value) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/finitio/type/builtin_type.rb', line 36

def include?(value)
  ruby_type === value
end

#resolve_proxies(system) ⇒ Object



56
57
58
# File 'lib/finitio/type/builtin_type.rb', line 56

def resolve_proxies(system)
  self
end

#to_json_schema(*args, &bl) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/finitio/json_schema/builtin_type.rb', line 18

def to_json_schema(*args, &bl)
  mapped = JsonSchema::BUILTIN_MAPPING[ruby_type]
  if mapped
    { type: mapped }
  else
    raise JsonSchema::Error, "Unable to map #{ruby_type} to json-schema"
  end
end