lib/bindata/base.rb in bindata-0.8.0 vs lib/bindata/base.rb in bindata-0.8.1
- old
+ new
@@ -63,10 +63,12 @@
end
alias_method :optional_parameter, :optional_parameters
# Returns both the mandatory and optional parameters used by this class.
def parameters
+ # warn about deprecated method - remove before releasing 1.0
+ warn "warning: #parameters is deprecated."
(mandatory_parameters + optional_parameters).uniq
end
# Instantiates this class and reads from +io+. For single value objects
# just the value is returned, otherwise the newly created data object is
@@ -110,33 +112,35 @@
#
# +params+ is a hash containing symbol keys. Some params may
# reference callable objects (methods or procs). +env+ is the
# environment that these callable objects are evaluated in.
def initialize(params = {}, env = nil)
+ # all known parameters
+ mandatory = self.class.mandatory_parameters
+ optional = self.class.optional_parameters
+
# default :readwrite param to true if unspecified
if not params.has_key?(:readwrite)
params = params.dup
params[:readwrite] = true
end
# ensure mandatory parameters exist
- self.class.mandatory_parameters.each do |prm|
+ mandatory.each do |prm|
if not params.has_key?(prm)
raise ArgumentError, "parameter ':#{prm}' must be specified " +
"in #{self}"
end
end
- known_params = self.class.parameters
-
# partition parameters into known and extra parameters
@params = {}
extra = {}
params.each do |k,v|
k = k.to_sym
raise ArgumentError, "parameter :#{k} is nil in #{self}" if v.nil?
- if known_params.include?(k)
+ if mandatory.include?(k) or optional.include?(k)
@params[k] = v.freeze
else
extra[k] = v.freeze
end
end
@@ -147,22 +151,26 @@
@env.data_object = self
end
# Returns the class matching a previously registered +name+.
def klass_lookup(name)
- klass = self.class.lookup(name)
- if klass.nil? and @env.parent_data_object != nil
- # lookup failed so retry in the context of the parent data object
- klass = @env.parent_data_object.klass_lookup(name)
+ @cache ||= {}
+ klass = @cache[name]
+ if klass.nil?
+ klass = self.class.lookup(name)
+ if klass.nil? and @env.parent_data_object != nil
+ # lookup failed so retry in the context of the parent data object
+ klass = @env.parent_data_object.klass_lookup(name)
+ end
+ @cache[name] = klass
end
klass
end
- # Returns a list of parameters that *weren't* provided to this object.
- def unsupplied_parameters
- supplied = @params.keys + @env.params.keys
- self.class.parameters - supplied
+ # Returns a list of parameters that are accepted by this object
+ def accepted_parameters
+ (self.class.mandatory_parameters + self.class.optional_parameters).uniq
end
# Reads data into this bin object by calling #do_read then #done_read.
def read(io)
# remove previous method to prevent warnings
@@ -216,10 +224,10 @@
# Returns the value of the evaluated parameter. +key+ references a
# parameter from the +params+ hash used when creating the data object.
# +values+ contains data that may be accessed when evaluating +key+.
# Returns nil if +key+ does not refer to any parameter.
- def eval_param(key, values = {})
+ def eval_param(key, values = nil)
@env.lazy_eval(@params[key], values)
end
# Returns the parameter from the +params+ hash referenced by +key+.
# Use this method if you are sure the parameter is not to be evaluated.