Class: Object

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

Overview

CORE PATCHES #####

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) meta_attr_accessor(*names)

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

+ (Object) meta_attr_reader(*names)



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

+ (Object) meta_attr_writer(*names)



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

- (Object) _attach_as_instance_variables(hash, options = {})

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



864
865
866
867
868
869
870
871
872
873
874
875
876
# File 'lib/rfm/utilities/sax_parser.rb', line 864

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(v)) if value_translator
			instance_variable_set("@#{k}", v)
		end
	end
end

- (Object) _attach_object!(obj, *args)

Master method to attach any object to this object.



767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
# File 'lib/rfm/utilities/sax_parser.rb', line 767

def _attach_object!(obj, *args) # name/label, collision-delimiter, attachment-prefs, type, *options: <options>
	default_options = {
		:shared_variable_name => 'attributes',
		:default_class => Hash,
		:create_accessors => [] #:all, :private, :shared, :hash
	}
	options = 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])
	#puts ['OBJECT_attach_object', type, name, self.class, obj.class, delimiter, prefs, options[:shared_variable_name], options[:default_class], options[:create_accessors]].join(', ')
	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

- (Object) _create_accessors(options = [])

We don't know which attributes are shared, so this isn't really accurate per the options.



849
850
851
852
853
854
855
856
857
858
859
# File 'lib/rfm/utilities/sax_parser.rb', line 849

def _create_accessors options=[]
	options=[options].flatten.compact
	#puts ['CREATE_ACCESSORS', self.class, options]
if (options & ['all', 'private']).any?
	meta = (class << self; self; end)
	meta.send(:attr_reader, *instance_variables.collect{|v| v.to_s[1,99].to_sym})
end
if (options & ['all', 'shared']).any?
	instance_variables.collect{|v| instance_variable_get(v)._create_accessors('hash')}
end
end

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

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



833
834
835
836
837
838
839
840
841
842
843
844
845
846
# File 'lib/rfm/utilities/sax_parser.rb', line 833

def _get_attribute(name, shared_var_name=nil, options={})
	return unless name
	#puts ["\nOBJECT_get_attribute", 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

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

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



809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
# File 'lib/rfm/utilities/sax_parser.rb', line 809

def _merge_instance!(obj, name, delimiter, prefs, type, options={})
	#puts ['_merge_instance!', self.class, obj.class, name, delimiter, prefs, type, options.keys, '_end_merge_instance!'].join(', ')
	if instance_variable_get("@#{name}") || delimiter
		if delimiter
			delimit_name = obj._get_attribute(delimiter, options[:shared_variable_name]).to_s.downcase
			#puts ['_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]
			instance_variable_set("@#{name}", [instance_variable_get("@#{name}")].flatten << obj)
		end
	else
		#puts ['_setting_new_instance_var', name]
		instance_variable_set("@#{name}", obj)
	end
end

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

Master method to merge any object with this object



789
790
791
792
793
794
795
796
# File 'lib/rfm/utilities/sax_parser.rb', line 789

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

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

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



799
800
801
802
803
804
805
806
# File 'lib/rfm/utilities/sax_parser.rb', line 799

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=>'attributes'))
end

- (Object) rfm_force_array

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

- (Object) rfm_local_methods

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