Class: Rigor::Type::HashShape
- Inherits:
-
Object
- Object
- Rigor::Type::HashShape
- Includes:
- AcceptanceRouter, PlainLattice, ValueSemantics
- Defined in:
- lib/rigor/type/hash_shape.rb
Overview
A hash shape with statically known keys. Inhabitants are Ruby ‘Hash` instances whose known entries inhabit the corresponding value types. RBS records correspond to the exact closed subset; Rigor extends that carrier with optional keys, read-only entry views, and an open/closed extra-key policy.
Keys are restricted to Symbol and String values. Exact closed symbol-keyed shapes erase to the RBS record syntax ‘{ a: Integer, ?b: String }`; all other shapes degrade to `Hash[K, V]` or raw `Hash` when no useful bounds are available.
Equality and hashing are structural over the (key -> Rigor::Type) pair set and policy fields. Hash insertion order is preserved by the underlying storage but does NOT affect equality (matching Ruby’s ‘Hash#==`).
See docs/type-specification/rbs-compatible-types.md (records) and docs/type-specification/rigor-extensions.md (hash shape).
Constant Summary collapse
- ALLOWED_KEY_CLASSES =
[Symbol, String].freeze
- EXTRA_KEY_POLICIES =
%i[open closed].freeze
- POLICY_KEYWORDS =
%i[required_keys optional_keys read_only_keys extra_keys].freeze
Instance Attribute Summary collapse
-
#extra_keys ⇒ Object
readonly
Returns the value of attribute extra_keys.
-
#optional_keys ⇒ Object
readonly
Returns the value of attribute optional_keys.
-
#pairs ⇒ Object
readonly
Returns the value of attribute pairs.
-
#read_only_keys ⇒ Object
readonly
Returns the value of attribute read_only_keys.
-
#required_keys ⇒ Object
readonly
Returns the value of attribute required_keys.
Instance Method Summary collapse
- #closed? ⇒ Boolean
- #describe(verbosity = :short) ⇒ Object
-
#erase_to_rbs ⇒ Object
Erases to the RBS record form ‘{ a: Integer, ?b: String }` for exact closed symbol-keyed shapes.
-
#initialize(pairs = nil, **keywords) ⇒ HashShape
constructor
A new instance of HashShape.
- #inspect ⇒ Object
- #open? ⇒ Boolean
- #optional_key?(key) ⇒ Boolean
- #read_only_key?(key) ⇒ Boolean
- #required_key?(key) ⇒ Boolean
Methods included from ValueSemantics
Methods included from AcceptanceRouter
Methods included from PlainLattice
Constructor Details
#initialize(pairs = nil, **keywords) ⇒ HashShape
Returns a new instance of HashShape.
51 52 53 54 55 56 57 58 |
# File 'lib/rigor/type/hash_shape.rb', line 51 def initialize(pairs = nil, **keywords) pairs, policy = split_constructor_args(pairs, keywords) validate_pairs!(pairs) @pairs = pairs.dup.freeze apply_policy!(policy) freeze end |
Instance Attribute Details
#extra_keys ⇒ Object (readonly)
Returns the value of attribute extra_keys.
33 34 35 |
# File 'lib/rigor/type/hash_shape.rb', line 33 def extra_keys @extra_keys end |
#optional_keys ⇒ Object (readonly)
Returns the value of attribute optional_keys.
33 34 35 |
# File 'lib/rigor/type/hash_shape.rb', line 33 def optional_keys @optional_keys end |
#pairs ⇒ Object (readonly)
Returns the value of attribute pairs.
33 34 35 |
# File 'lib/rigor/type/hash_shape.rb', line 33 def pairs @pairs end |
#read_only_keys ⇒ Object (readonly)
Returns the value of attribute read_only_keys.
33 34 35 |
# File 'lib/rigor/type/hash_shape.rb', line 33 def read_only_keys @read_only_keys end |
#required_keys ⇒ Object (readonly)
Returns the value of attribute required_keys.
33 34 35 |
# File 'lib/rigor/type/hash_shape.rb', line 33 def required_keys @required_keys end |
Instance Method Details
#closed? ⇒ Boolean
84 85 86 |
# File 'lib/rigor/type/hash_shape.rb', line 84 def closed? extra_keys == :closed end |
#describe(verbosity = :short) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/rigor/type/hash_shape.rb', line 60 def describe(verbosity = :short) return "{}" if pairs.empty? rendered = pairs.map { |k, v| render_entry(k, v, verbosity) } rendered << "..." if open? "{ #{rendered.join(', ')} }" end |
#erase_to_rbs ⇒ Object
Erases to the RBS record form ‘{ a: Integer, ?b: String }` for exact closed symbol-keyed shapes. Open shapes and string-keyed closed shapes degrade to a generic Hash bound.
71 72 73 74 75 76 77 78 |
# File 'lib/rigor/type/hash_shape.rb', line 71 def erase_to_rbs return "{}" if pairs.empty? && closed? return hash_erasure unless closed? return hash_erasure if pairs.each_key.any? { |k| !k.is_a?(Symbol) } rendered = pairs.map { |k, v| "#{record_key(k)}: #{v.erase_to_rbs}" } "{ #{rendered.join(', ')} }" end |
#inspect ⇒ Object
108 109 110 |
# File 'lib/rigor/type/hash_shape.rb', line 108 def inspect "#<Rigor::Type::HashShape #{describe(:short)}>" end |
#open? ⇒ Boolean
80 81 82 |
# File 'lib/rigor/type/hash_shape.rb', line 80 def open? extra_keys == :open end |
#optional_key?(key) ⇒ Boolean
92 93 94 |
# File 'lib/rigor/type/hash_shape.rb', line 92 def optional_key?(key) optional_keys.include?(key) end |
#read_only_key?(key) ⇒ Boolean
96 97 98 |
# File 'lib/rigor/type/hash_shape.rb', line 96 def read_only_key?(key) read_only_keys.include?(key) end |
#required_key?(key) ⇒ Boolean
88 89 90 |
# File 'lib/rigor/type/hash_shape.rb', line 88 def required_key?(key) required_keys.include?(key) end |