Class: Hash

Inherits:
Object show all
Defined in:
lib/rfm/utilities/core_ext.rb,
lib/rfm/utilities/sax_parser.rb

Overview

Array

Direct Known Subclasses

Rfm::CaseInsensitiveHash

Instance Method Summary collapse

Instance Method Details

#_create_accessor(name) ⇒ Object

def _create_accessors options=[] #puts ['CREATE_ACCESSORS_for_HASH', self.class, options] options=.flatten.compact super and if (options & ['all', 'hash']).any? meta = (class << self; self; end) keys.each do |k| meta.send(:define_method, k) do self end end end end


1102
1103
1104
1105
1106
1107
1108
# File 'lib/rfm/utilities/sax_parser.rb', line 1102

def _create_accessor(name)
	#puts "HASH._create_accessor '#{name}' for Hash '#{self.class}'"
	meta = (class << self; self; end)
	meta.send(:define_method, name) do
		self[name]
	end
end

#_merge_object!(obj, name, delimiter, prefs, type, options = {}) ⇒ Object


1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
# File 'lib/rfm/utilities/sax_parser.rb', line 1054

def _merge_object!(obj, name, delimiter, prefs, type, options={})
	#puts ["\n*****HASH._merge_object", "type: #{type}", "name: #{name}", "self.class: #{self.class}", "new_obj: #{(obj.to_s rescue obj.class)}", "delimiter: #{delimiter}", "prefs: #{prefs}", "options: #{options}"]
	case
	when prefs=='shared'
		_merge_shared!(obj, name, delimiter, prefs, type, options)
	when prefs=='private'
		_merge_instance!(obj, name, delimiter, prefs, type, options)
	when (self[name] || delimiter)
		rslt = if delimiter
			delimit_name =  obj._get_attribute(delimiter, options[:shared_variable_name]).to_s.downcase
			#puts "MERGING delimited object with hash: self '#{self.class}' obj '#{obj.class}' name '#{name}' delim '#{delimiter}' delim_name '#{delimit_name}' options '#{options}'"
			self[name] ||= options[:default_class].new
			#self[name][delimit_name]=obj
			# This is supposed to handle multiple elements who's delimiter value is the SAME.
			self[name]._merge_delimited_object!(obj, delimit_name)
		else
			if name == options[:text_label]
				self[name] << obj.to_s
			else
				self[name] = [self[name]].flatten
				self[name] << obj
			end
		end
		_create_accessor(name) if (options[:create_accessors].to_a & ['all','shared','hash']).any?
		
		rslt
	else
		rslt = self[name] = obj
		_create_accessor(name) if (options[:create_accessors] & ['all','shared','hash']).any?
		#puts ["\nRESULT", self.to_yaml]
		rslt
	end
end

#rfm_filter(*args) ⇒ Object


124
125
126
127
128
129
130
# File 'lib/rfm/utilities/core_ext.rb', line 124

def rfm_filter(*args)
	options = args.rfm_extract_options!
	delete = options[:delete]
	self.dup.each_key do |k|
		self.delete(k) if (delete ? args.include?(k) : !args.include?(k))
	end
end

#rfm_only(*keepers) ⇒ Object

TODO: Possibly deprecated, delete if not used.


120
121
122
# File 'lib/rfm/utilities/core_ext.rb', line 120

def rfm_only(*keepers)
	self.dup.each_key {|k| self.delete(k) if !keepers.include?(k)}
end

#to_cihObject

Convert hash to Rfm::CaseInsensitiveHash


133
134
135
136
137
# File 'lib/rfm/utilities/core_ext.rb', line 133

def to_cih
	new = Rfm::CaseInsensitiveHash.new
	self.each{|k,v| new[k] = v}
	new
end