Class: Errgonomic::VariantName
Overview
What a bare Some, None, Ok or Err evaluates to. Rust writes a
value as return None, where here the value is None(); the bare name
is for patterns. It matches as the class it names, and refuses to become
a string, so a missing pair of parentheses raises rather than writing a
class name into a column. It is not a class, so an application's own
class None fails where it is written instead of reopening the gem's.
Class Method Summary collapse
-
.define(name, variant) ⇒ Object
Name a variant at top level, refusing a constant the application already holds there rather than replacing it.
Instance Method Summary collapse
- #===(other) ⇒ Object
-
#initialize(name, variant) ⇒ VariantName
constructor
A new instance of VariantName.
- #inspect ⇒ Object
- #names?(variant) ⇒ Boolean
-
#refuse!(*_args) ⇒ Object
(also: #to_s, #to_json, #as_json)
Refuse to stand in for a value.
Constructor Details
#initialize(name, variant) ⇒ VariantName
Returns a new instance of VariantName.
36 37 38 39 40 |
# File 'lib/errgonomic/variant_name.rb', line 36 def initialize(name, variant) @name = name @variant = variant freeze end |
Class Method Details
.define(name, variant) ⇒ Object
Name a variant at top level, refusing a constant the application already holds there rather than replacing it.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/errgonomic/variant_name.rb', line 25 def self.define(name, variant) if Object.const_defined?(name, false) existing = Object.const_get(name) return if existing.is_a?(VariantName) && existing.names?(variant) raise NameError.new("#{name} is already defined as #{existing.inspect}; errgonomic defines #{name} " \ "at top level to name #{variant} in patterns, so rename the application's constant", name) end Object.const_set(name, new(name, variant)) end |
Instance Method Details
#===(other) ⇒ Object
46 47 48 |
# File 'lib/errgonomic/variant_name.rb', line 46 def ===(other) @variant === other # rubocop:disable Style/CaseEquality end |
#inspect ⇒ Object
50 51 52 |
# File 'lib/errgonomic/variant_name.rb', line 50 def inspect @variant.inspect end |
#names?(variant) ⇒ Boolean
42 43 44 |
# File 'lib/errgonomic/variant_name.rb', line 42 def names?(variant) @variant.equal?(variant) end |
#refuse!(*_args) ⇒ Object Also known as: to_s, to_json, as_json
Refuse to stand in for a value.
55 56 57 58 |
# File 'lib/errgonomic/variant_name.rb', line 55 def refuse!(*_args) raise Errgonomic::SerializeError, "bare #{@name} names a variant for a pattern, not a value; build one with parentheses" end |