class NRSER::Types::Shape

Create a {Shape} type that parameterizes {#pairs} of object keys and {Type} values.

Members of the type are values `v` for which for all keys `k` and paired value types `t_k` `v` is a member of `t_k`:

shape.pairs.all? { |k, t_k| t_k.test? v[k] }

@note

Construct shape types using the {.Shape} factory.

Attributes

pairs[R]

TODO document `pairs` attribute.

@return [Hash]

Public Class Methods

new(pairs, **options) click to toggle source

Instantiate a new `NRSER::Types::Shape`.

Calls superclass method
# File lib/nrser/types/shape.rb, line 51
def initialize pairs, **options
  super **options
  @pairs = pairs.map { |k, v|
    [k, NRSER::Types.make( v )]
  }.to_h.freeze
end

Public Instance Methods

custom_from_data(data) click to toggle source
# File lib/nrser/types/shape.rb, line 108
def custom_from_data data
  pairs.map { |key, type|
    [ key, type.from_data( data[key] ) ]
  }.to_h
end
default_symbolic() click to toggle source
# File lib/nrser/types/shape.rb, line 91
def default_symbolic
  string_format pre: '{', post: '}', method: :symbolic
end
explain() click to toggle source
# File lib/nrser/types/shape.rb, line 96
def explain
  string_format pre: 'Shape<', post: '>', method: :explain
end
has_from_data?() click to toggle source

@!endgroup Display Instance Methods # ************************************

# File lib/nrser/types/shape.rb, line 103
def has_from_data?
  pairs.values.all? { |type| type.has_from_data? }
end
string_format(pre:, post:, method:, spaces: true) click to toggle source

@!group Display Instance Methods


# File lib/nrser/types/shape.rb, line 74
def string_format pre:, post:, method:, spaces: true
  space = spaces ? ' ' : ''

  inner = @pairs.map { |k, v|
    key_part = if k.is_a? Symbol
      "#{ k }:#{ space }"
    else
      "#{ k.inspect }#{ space }=>#{ space }"
    end

    key_part + v.public_send( method )
  }

  pre + inner.join( ",#{ space }" ) + post
end
test?(value) click to toggle source

Instance Methods

# File lib/nrser/types/shape.rb, line 62
def test? value
  begin
    @pairs.all? { |k, v| v === value[k] }
  rescue
    false
  end
end