Class: TypedVariable
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
attr_public_read_private_write
Constructor Details
#initialize(type, value = nil, on_change: nil, **options) ⇒ TypedVariable
Returns a new instance of TypedVariable.
8
9
10
11
12
|
# File 'lib/0xfacet/typed/typed_variable.rb', line 8
def initialize(type, value = nil, on_change: nil, **options)
self.type = type
self.value = value || type.default_value
self.on_change = on_change
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/0xfacet/typed/typed_variable.rb', line 76
def method_missing(name, *args, &block)
if value.respond_to?(name)
result = value.send(name, *args, &block)
if result.class == value.class
begin
result = type.check_and_normalize_literal(result)
rescue ContractErrors::VariableTypeError => e
if type.is_uint?
result = TypedVariable.create(:uint256, result)
else
raise
end
end
end
result
if name.to_s.end_with?("=") && !%w[>= <=].include?(name.to_s[-2..])
self.value = result if type.is_value_type?
self
else
result
end
else
super
end
end
|
Instance Attribute Details
#on_change ⇒ Object
Returns the value of attribute on_change.
5
6
7
|
# File 'lib/0xfacet/typed/typed_variable.rb', line 5
def on_change
@on_change
end
|
#value ⇒ Object
Returns the value of attribute value.
5
6
7
|
# File 'lib/0xfacet/typed/typed_variable.rb', line 5
def value
@value
end
|
Class Method Details
.create(type, value = nil, on_change: nil, **options) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/0xfacet/typed/typed_variable.rb', line 14
def self.create(type, value = nil, on_change: nil, **options)
type = Type.create(type)
options[:on_change] = on_change
if type.mapping?
MappingType.new(type, value, **options)
elsif type.array?
ArrayType.new(type, value, **options)
elsif type.contract?
ContractType.new(type, value, **options)
else
new(type, value, **options)
end
end
|
.create_or_validate(type, value = nil, on_change: nil) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/0xfacet/typed/typed_variable.rb', line 30
def self.create_or_validate(type, value = nil, on_change: nil)
if value.is_a?(TypedVariable)
unless Type.create(type).can_be_assigned_from?(value.type)
raise VariableTypeError.new("invalid #{type}: #{value.inspect}")
end
value = value.value
end
create(type, value, on_change: on_change)
end
|
Instance Method Details
#!=(other) ⇒ Object
109
110
111
|
# File 'lib/0xfacet/typed/typed_variable.rb', line 109
def !=(other)
!(self == other)
end
|
#==(other) ⇒ Object
113
114
115
116
117
118
119
120
|
# File 'lib/0xfacet/typed/typed_variable.rb', line 113
def ==(other)
if other.is_a?(self.class)
return false unless type.values_can_be_compared?(other.type)
return value == other.value
else
return value == TypedVariable.create(type, other).value
end
end
|
#as_json(args = {}) ⇒ Object
42
43
44
|
# File 'lib/0xfacet/typed/typed_variable.rb', line 42
def as_json(args = {})
serialize
end
|
#deserialize(serialized_value) ⇒ Object
58
59
60
|
# File 'lib/0xfacet/typed/typed_variable.rb', line 58
def deserialize(serialized_value)
self.value = serialized_value
end
|
#eql?(other) ⇒ Boolean
126
127
128
|
# File 'lib/0xfacet/typed/typed_variable.rb', line 126
def eql?(other)
hash == other.hash
end
|
#hash ⇒ Object
122
123
124
|
# File 'lib/0xfacet/typed/typed_variable.rb', line 122
def hash
[value, type].hash
end
|
#respond_to_missing?(name, include_private = false) ⇒ Boolean
105
106
107
|
# File 'lib/0xfacet/typed/typed_variable.rb', line 105
def respond_to_missing?(name, include_private = false)
value.respond_to?(name, include_private) || super
end
|
#serialize ⇒ Object
46
47
48
|
# File 'lib/0xfacet/typed/typed_variable.rb', line 46
def serialize
value
end
|
#to_s ⇒ Object
50
51
52
53
54
55
56
|
# File 'lib/0xfacet/typed/typed_variable.rb', line 50
def to_s
if value.is_a?(String) || value.is_a?(Integer)
value.to_s
else
raise "No string conversion"
end
end
|