lib/bindata/single.rb in bindata-0.9.0 vs lib/bindata/single.rb in bindata-0.9.1
- old
+ new
@@ -69,59 +69,30 @@
# Returns if the value of this data has been read or explicitly set.
def clear?
@value.nil?
end
- # Reads the value for this data from +io+.
- def _do_read(io)
- @in_read = true
- @value = read_val(io)
-
- # does the value meet expectations?
- if has_param?(:check_value)
- current_value = self.value
- expected = eval_param(:check_value, :value => current_value)
- if not expected
- raise ValidityError, "value not as expected"
- elsif current_value != expected and expected != true
- raise ValidityError, "value is '#{current_value}' but " +
- "expected '#{expected}'"
- end
- end
+ # Single objects are single_values
+ def single_value?
+ true
end
- # To be called after calling #do_read.
- def done_read
- @in_read = false
+ # Single objects don't contain fields so this returns an empty list.
+ def field_names
+ []
end
- # Writes the value for this data to +io+.
- def _write(io)
- raise "can't write whilst reading" if @in_read
- io.write(val_to_str(_value))
- end
-
- # Returns the number of bytes it will take to write this data.
- def _num_bytes(ignored)
- val_to_str(_value).length
- end
-
# Returns a snapshot of this data object.
def snapshot
value
end
- # Single objects are single_values
- def single_value?
- true
+ # To be called after calling #do_read.
+ def done_read
+ @in_read = false
end
- # Single objects don't contain fields so this returns an empty list.
- def field_names
- []
- end
-
# Returns the current value of this data.
def value
_value
end
@@ -129,16 +100,49 @@
def value=(v)
# only allow modification if the value isn't predefined
unless has_param?(:value)
raise ArgumentError, "can't set a nil value" if v.nil?
@value = v
+
+ # Note that this doesn't do anything in ruby 1.8.x so ignore for now
+ # # explicitly return the output of #value as v may be different
+ # self.value
end
end
#---------------
private
+ # Reads the value for this data from +io+.
+ def _do_read(io)
+ @in_read = true
+ @value = read_val(io)
+
+ # does the value meet expectations?
+ if has_param?(:check_value)
+ current_value = self.value
+ expected = eval_param(:check_value, :value => current_value)
+ if not expected
+ raise ValidityError, "value not as expected"
+ elsif current_value != expected and expected != true
+ raise ValidityError, "value is '#{current_value}' but " +
+ "expected '#{expected}'"
+ end
+ end
+ end
+
+ # Writes the value for this data to +io+.
+ def _do_write(io)
+ raise "can't write whilst reading" if @in_read
+ io.writebytes(val_to_str(_value))
+ end
+
+ # Returns the number of bytes it will take to write this data.
+ def _do_num_bytes(ignored)
+ val_to_str(_value).length
+ end
+
# The unmodified value of this data object. Note that #value calls this
# method. This is so that #value can be overridden in subclasses to
# modify the value.
def _value
# Table of possible preconditions and expected outcome
@@ -155,24 +159,9 @@
else
# combining all other rules gives this simplified expression
@value || eval_param(:value) ||
eval_param(:initial_value) || sensible_default()
end
- end
-
- # Usuable by subclasses
-
- # Reads exactly +n+ bytes from +io+. This should be used by subclasses
- # in preference to <tt>io.read(n)</tt>.
- #
- # If the data read is nil an EOFError is raised.
- #
- # If the data read is too short an IOError is raised.
- def readbytes(io, n)
- str = io.read(n)
- raise EOFError, "End of file reached" if str == nil
- raise IOError, "data truncated" if str.size < n
- str
end
###########################################################################
# To be implemented by subclasses