Class: Literal::Value
- Inherits:
-
Object
- Object
- Literal::Value
- Defined in:
- lib/literal/value.rb
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
- .[](value) ⇒ Object
-
.delegate(*methods) ⇒ Object
Takes a list of method names and delegates them to the underlying value.
- .from_pack(payload) ⇒ Object
- .to_proc ⇒ Object
Instance Method Summary collapse
- #===(other) ⇒ Object (also: #==)
- #as_pack ⇒ Object
-
#initialize(value) ⇒ Value
constructor
A new instance of Value.
- #inspect ⇒ Object
- #marshal_dump ⇒ Object
- #marshal_load(payload) ⇒ Object
Constructor Details
#initialize(value) ⇒ Value
Returns a new instance of Value.
31 32 33 34 35 |
# File 'lib/literal/value.rb', line 31 def initialize(value) Literal.check(value, __type__) @value = value freeze end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
37 38 39 |
# File 'lib/literal/value.rb', line 37 def value @value end |
Class Method Details
.[](value) ⇒ Object
8 9 10 |
# File 'lib/literal/value.rb', line 8 def self.[](value) new(value) end |
.delegate(*methods) ⇒ Object
Takes a list of method names and delegates them to the underlying value.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/literal/value.rb', line 19 def self.delegate(*methods) methods.each do |method_name| class_eval(" # frozen_string_literal: true\n\n def \#{method_name}(...)\n @value.\#{method_name}(...)\n end\n RUBY\n end\nend\n", __FILE__, __LINE__ + 1) |
.from_pack(payload) ⇒ Object
12 13 14 15 16 |
# File 'lib/literal/value.rb', line 12 def self.from_pack(payload) object = allocate object.marshal_load(payload) object end |
.to_proc ⇒ Object
4 5 6 |
# File 'lib/literal/value.rb', line 4 def self.to_proc -> (value) { new(value) } end |
Instance Method Details
#===(other) ⇒ Object Also known as: ==
43 44 45 |
# File 'lib/literal/value.rb', line 43 def ===(other) self.class === other && @value == other.value end |
#as_pack ⇒ Object
49 50 51 |
# File 'lib/literal/value.rb', line 49 def as_pack marshal_dump end |
#inspect ⇒ Object
39 40 41 |
# File 'lib/literal/value.rb', line 39 def inspect "#{self.class.name}(#{value.inspect})" end |
#marshal_dump ⇒ Object
60 61 62 |
# File 'lib/literal/value.rb', line 60 def marshal_dump [1, @value, frozen?].freeze end |
#marshal_load(payload) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/literal/value.rb', line 53 def marshal_load(payload) _version, value, was_frozen = payload @value = value freeze if was_frozen end |