Class: Value

Inherits:
Object show all
Defined in:
lib/primitive_wrapper.rb

Overview

BASE CLASS == Value … a wrapper for any ruby object

Direct Known Subclasses

Bool, Property, SymbolW, ValueAdd

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj = nil) ⇒ Value

Returns a new instance of Value.



151
152
153
154
155
# File 'lib/primitive_wrapper.rb', line 151

def initialize(obj=nil)
  obj = obj.prim_value
  ensure_valid(obj)
  @value = obj
end

Class Method Details

.freeze_raise?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/primitive_wrapper.rb', line 146

def self.freeze_raise?
  @freeze_raise
end

.ignore_on_freezeObject



143
144
145
# File 'lib/primitive_wrapper.rb', line 143

def self.ignore_on_freeze
 @freeze_raise = false
end

.raise_on_freezeObject



140
141
142
# File 'lib/primitive_wrapper.rb', line 140

def self.raise_on_freeze
 @freeze_raise = true
end

Instance Method Details

#!=(other) ⇒ Object



209
210
211
# File 'lib/primitive_wrapper.rb', line 209

def !=(other)
  other.prim_value != @value
end

#==(other) ⇒ Object



205
206
207
# File 'lib/primitive_wrapper.rb', line 205

def ==(other)
  other.prim_value == @value
end

#ensure_valid(obj, mess = "Incompatible type") ⇒ Object



175
176
177
178
179
# File 'lib/primitive_wrapper.rb', line 175

def ensure_valid(obj, mess = "Incompatible type")
  unless valid_type(obj)
    raise mess
  end  
end

#freezeObject

never allow the wrapper to be frozen



181
182
183
# File 'lib/primitive_wrapper.rb', line 181

def freeze  # never allow the wrapper to be frozen
  raise "this object cannot be frozen" if self.class.freeze_raise?
end

#inspectObject



225
226
227
228
# File 'lib/primitive_wrapper.rb', line 225

def inspect
  str = @value.nil? ? "nil" : @value.inspect
  "(#{self.class}==>#{str})"
end

#prim_valueObject



197
198
199
# File 'lib/primitive_wrapper.rb', line 197

def prim_value
  @value
end

#replace(other) ⇒ Object



217
218
219
# File 'lib/primitive_wrapper.rb', line 217

def replace(other)    
  @value = other.prim_value if valid_type(other)
end

#to_sObject



221
222
223
# File 'lib/primitive_wrapper.rb', line 221

def to_s
  @value.to_s
end

#to_wrapperObject



157
158
159
# File 'lib/primitive_wrapper.rb', line 157

def to_wrapper
  self
end

#typeObject



171
172
173
# File 'lib/primitive_wrapper.rb', line 171

def type
  @value.class
end

#type_of?(cls) ⇒ Boolean

Returns:

  • (Boolean)


165
166
167
168
169
# File 'lib/primitive_wrapper.rb', line 165

def type_of?(cls)
  return true if @value.kind_of? cls
  return true if self.kind_of? cls
  false
end

#unwrapObject



201
202
203
# File 'lib/primitive_wrapper.rb', line 201

def unwrap
  @value
end

#valObject



185
186
187
# File 'lib/primitive_wrapper.rb', line 185

def val
  @value
end

#val=(dat) ⇒ Object



193
194
195
# File 'lib/primitive_wrapper.rb', line 193

def val=(dat)
  replace(dat)
end

#valid_type(prm = nil) ⇒ Object



213
214
215
# File 'lib/primitive_wrapper.rb', line 213

def valid_type(prm=nil)
  true
end

#wrapped?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/primitive_wrapper.rb', line 161

def wrapped?
  true
end

#~Object



189
190
191
# File 'lib/primitive_wrapper.rb', line 189

def ~
  @value
end