Class: Literal::Value

Inherits:
Object
  • Object
show all
Defined in:
lib/literal/value.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#valueObject (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_procObject



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_packObject



49
50
51
# File 'lib/literal/value.rb', line 49

def as_pack
  marshal_dump
end

#inspectObject



39
40
41
# File 'lib/literal/value.rb', line 39

def inspect
  "#{self.class.name}(#{value.inspect})"
end

#marshal_dumpObject



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