Class: Object

Inherits:
BasicObject
Defined in:
lib/rfm/utilities/sax_parser.rb,
lib/rfm/utilities/core_ext.rb

Overview

CORE ADDITIONS #####

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.meta_attr_accessor(*names) ⇒ Object

Adds methods to put instance variables in rfm_metaclass, plus getter/setters This is useful to hide instance variables in objects that would otherwise show “too much” information.



14
15
16
17
# File 'lib/rfm/utilities/core_ext.rb', line 14

def self.meta_attr_accessor(*names)
meta_attr_reader(*names)
meta_attr_writer(*names)
end

.meta_attr_reader(*names) ⇒ Object



19
20
21
22
23
# File 'lib/rfm/utilities/core_ext.rb', line 19

def self.meta_attr_reader(*names)
  names.each do |n|
    define_method(n.to_s) {rfm_metaclass.instance_variable_get("@#{n}")}
  end
end

.meta_attr_writer(*names) ⇒ Object



25
26
27
28
29
# File 'lib/rfm/utilities/core_ext.rb', line 25

def self.meta_attr_writer(*names)
  names.each do |n|
    define_method(n.to_s + "=") {|val| rfm_metaclass.instance_variable_set("@#{n}", val)}
  end
end

Instance Method Details

#_attach_as_instance_variables(hash, options = {}) ⇒ Object

Attach hash as individual instance variables to self. This is for manually attaching a hash of attributes to the current object. Pass in translation procs to alter the keys or values.



1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
# File 'lib/rfm/utilities/sax_parser.rb', line 1016

def _attach_as_instance_variables(hash, options={})
	#hash.each{|k,v| instance_variable_set("@#{k}", v)} if hash.is_a? Hash
	key_translator = options[:key_translator]
	value_translator = options[:value_translator]
	#puts ["ATTACH_AS_INSTANCE_VARIABLES", key_translator, value_translator].join(', ')
	if hash.is_a? Hash
		hash.each do |k,v|
			(k = key_translator.call(k)) if key_translator
			(v = value_translator.call(k, v)) if value_translator
			instance_variable_set("@#{k}", v)
		end
	end
end

#_attach_object!(obj, *args) ⇒ Object

Master method to attach any object to this object.



894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
# File 'lib/rfm/utilities/sax_parser.rb', line 894

def _attach_object!(obj, *args) # name/label, collision-delimiter, attachment-prefs, type, *options: <options>
	#puts ["\nATTACH_OBJECT._attach_object", self.class, obj.class, obj.to_s, args].to_yaml
	options = ATTACH_OBJECT_DEFAULT_OPTIONS.merge(args.last.is_a?(Hash) ? args.pop : {}){|key, old, new| new || old}
	# 	name = (args[0] || options[:name])
	# 	delimiter = (args[1] || options[:delimiter])
	prefs = (args[2] || options[:prefs])
	# 	type = (args[3] || options[:type])
	return if (prefs=='none' || prefs=='cursor')   #['none', 'cursor'].include? prefs ... not sure which is faster.
	self._merge_object!(
		obj,
		args[0] || options[:name] || 'unknown_name',
		args[1] || options[:delimiter],
		prefs,
		args[3] || options[:type],
		options
	)
	
# 		case
# 		when prefs=='none' || prefs=='cursor'; nil
# 		when name
# 			self._merge_object!(obj, name, delimiter, prefs, type, options)
# 		else
# 			self._merge_object!(obj, 'unknown_name', delimiter, prefs, type, options)
# 		end
end

#_create_accessor(name) ⇒ Object

NEW



1007
1008
1009
1010
1011
# File 'lib/rfm/utilities/sax_parser.rb', line 1007

def _create_accessor(name)
  #puts "OBJECT._create_accessor '#{name}' for Object '#{self.class}'"
 	meta = (class << self; self; end)
	meta.send(:attr_reader, name.to_sym)
end

#_get_attribute(name, shared_var_name = nil, options = {}) ⇒ Object

Get an instance variable, a member of a shared instance variable, or a hash value of self.



974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
# File 'lib/rfm/utilities/sax_parser.rb', line 974

def _get_attribute(name, shared_var_name=nil, options={})
	return unless name
	#puts ["\n\n", self.to_yaml]
	#puts ["OBJECT_get_attribute", self.class, self.instance_variables, name, shared_var_name, options].join(', ')
	(shared_var_name = options[:shared_variable_name]) unless shared_var_name

rslt = case
when self.is_a?(Hash) && self[name]; self[name]
when ((var= instance_variable_get("@#{shared_var_name}")) && var[name]); var[name]
else instance_variable_get("@#{name}")
end

#puts "OBJECT#_get_attribute: name '#{name}' shared_var_name '#{shared_var_name}' options '#{options}' rslt '#{rslt}'"
rslt
end

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

Merge a named object with the specified instance variable of self.



941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
# File 'lib/rfm/utilities/sax_parser.rb', line 941

def _merge_instance!(obj, name, delimiter, prefs, type, options={})
	#puts ["\n_merge_instance!", self.class, obj.class, name, delimiter, prefs, type, options.keys, '_end_merge_instance!'].join(', ')
	rslt = if instance_variable_get("@#{name}") || delimiter
		if delimiter
			delimit_name = obj._get_attribute(delimiter, options[:shared_variable_name]).to_s.downcase
			#puts ["\n_setting_with_delimiter", delimit_name]
			#instance_variable_set("@#{name}", instance_variable_get("@#{name}") || options[:default_class].new)[delimit_name]=obj
			# This line is more efficient than the above line.
			instance_variable_set("@#{name}", options[:default_class].new) unless instance_variable_get("@#{name}")
			instance_variable_get("@#{name}")[delimit_name]=obj
			# Trying to handle multiple portals with same table-occurance on same layout.
			# In parsing terms, this is trying to handle multiple elements who's delimiter field contains the SAME delimiter data.
			#instance_variable_get("@#{name}")._merge_object!(obj, delimit_name, nil, nil, nil)
		else
			#puts ["\_setting_existing_instance_var", name]
			if name == options[:text_label]
				instance_variable_get("@#{name}") << obj.to_s
			else
				instance_variable_set("@#{name}", [instance_variable_get("@#{name}")].flatten << obj)
			end
		end
	else
		#puts ["\n_setting_new_instance_var", name]
		instance_variable_set("@#{name}", obj)
	end
	
	# NEW
	_create_accessor(name) if (options[:create_accessors] & ['all','private']).any?
	
	rslt
end

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

Master method to merge any object with this object



921
922
923
924
925
926
927
928
# File 'lib/rfm/utilities/sax_parser.rb', line 921

def _merge_object!(obj, name, delimiter, prefs, type, options={})
	#puts ["\n-----OBJECT._merge_object", self.class, (obj.to_s rescue obj.class), name, delimiter, prefs, type.capitalize, options].join(', ')
	if prefs=='private'
		_merge_instance!(obj, name, delimiter, prefs, type, options)
	else
		_merge_shared!(obj, name, delimiter, prefs, type, options)
	end
end

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

Merge a named object with the shared instance variable of self.



931
932
933
934
935
936
937
938
# File 'lib/rfm/utilities/sax_parser.rb', line 931

def _merge_shared!(obj, name, delimiter, prefs, type, options={})
	shared_var = instance_variable_get("@#{options[:shared_variable_name]}") || instance_variable_set("@#{options[:shared_variable_name]}", options[:default_class].new)
	#puts "\n-----OBJECT._merge_shared: self '#{self.class}' obj '#{obj.class}' name '#{name}' delimiter '#{delimiter}' type '#{type}' shared_var '#{options[:shared_variable_name]} - #{shared_var.class}'"
	# TODO: Figure this part out:
	# The resetting of shared_variable_name to 'attributes' was to fix Asset.field_controls (it was not able to find the valuelive name).
	# I think there might be a level of heirarchy that is without a proper cursor model, when using shared variables & object delimiters.
	shared_var._merge_object!(obj, name, delimiter, nil, type, options.merge(:shared_variable_name=>ATTACH_OBJECT_DEFAULT_OPTIONS[:shared_variable_name]))
end

#rfm_force_arrayObject

since XmlMini doesn't know which will be returnd for any particular element. See Rfm Layout & Record where this is used.



34
35
36
37
# File 'lib/rfm/utilities/core_ext.rb', line 34

def rfm_force_array
	return [] if self.nil?
	self.is_a?(Array) ? self : [self]
end

#rfm_local_methodsObject

Just testing this functionality



40
41
42
# File 'lib/rfm/utilities/core_ext.rb', line 40

def rfm_local_methods
	self.methods - self.class.superclass.methods
end