class Struct
Alteration to Struct for support of structural processing:
Public Class Methods
process(the_struct=::Struct.new(nil),&block)
click to toggle source
# File lib/gxg/gxg_augmented.rb, line 3162 def self.process(the_struct=::Struct.new(nil),&block) new_struct = ::Struct.new(nil) if block.respond_to?(:call) if the_struct.is_any?(::Array, ::Hash, ::Struct, ::GxG::Database::PersistedHash, ::GxG::Database::PersistedArray) new_struct = the_struct.process!(&block) else raise ArgumentError, "you must pass a Hash, or an Array, or a ByteArray, or a Set, or a Struct" end end new_struct end
search(the_struct=::Struct.new(nil),&block)
click to toggle source
# File lib/gxg/gxg_augmented.rb, line 3174 def self.search(the_struct=::Struct.new(nil),&block) new_struct = ::Struct.new(nil) if block.respond_to?(:call) if the_struct.is_any?(::Array, ::Hash, ::Set, ::Struct, ::GxG::Database::PersistedHash, ::GxG::Database::PersistedArray) new_struct = the_struct.search(&block) else raise ArgumentError, "you must pass a Hash, or an Array, or a ByteArray, or a Set, or a Struct" end end new_struct end
Public Instance Methods
clone()
click to toggle source
# File lib/gxg/gxg_augmented.rb, line 3158 def clone() initialize_clone end
initialize_clone()
click to toggle source
# File lib/gxg/gxg_augmented.rb, line 3149 def initialize_clone result = self.class.new self.each_pair do |key, value| result[(key)] = value.clone end result end
Also aliased as: initialize_dup, dup
iterative(&block)
click to toggle source
def each_pair(&block)
# ok, bizarre : this method is listed in instance_methods but is missing when called. hrm. collection = {} self.members.to_enum.each do |key| collection[(key)] = (self[(key)]) end if block.respond_to?(:call) collection.to_enum(:each_pair).each do |key,value| block.call(key,value) end else collection.to_enum(:each_pair) end
end
# File lib/gxg/gxg_augmented.rb, line 3201 def iterative(&block) result = [] visit = Proc.new do |the_node=nil, accumulator=[]| node_stack = [] if the_node node_stack << ({:parent => nil, :parent_selector => nil, :object => (the_node)}) while (node_stack.size > 0) do a_node = node_stack.shift # if a_node[:object].is_any?(::Hash, ::Struct, ::GxG::Database::PersistedHash) a_node[:object].each_pair do |the_key, the_value| node_stack << ({:parent => a_node[:object], :parent_selector => the_key, :object => the_value}) end end if a_node[:object].is_any?(::Array, ::GxG::Database::PersistedArray) a_node[:object].each_with_index do |the_value, the_index| node_stack << ({:parent => a_node[:object], :parent_selector => the_index, :object => the_value}) end end # accumulator << a_node end end accumulator end # children_of = Proc.new do |the_db=[], the_parent=nil| list = [] the_db.each do |node| if node[:parent].object_id == the_parent.object_id list << node end end list end # begin database = visit.call(self,[]) link_db = children_of.call(database, self) if block.respond_to?(:call) while (link_db.size > 0) do entry = link_db.shift unless entry[:object].object_id == self.object_id # calls with parameters: the_value, the_key/the_index (the_selector), the_container raw_result = block.call(entry[:object], entry[:parent_selector], entry[:parent]) if raw_result result << raw_result end end if entry[:object].object_id != nil.object_id children = children_of.call(database, entry[:object]) children.each do |child| link_db << child end end end end # rescue Exception => the_error log_error({:error => the_error, :parameters => {}}) end # result end
process(&block)
click to toggle source
# File lib/gxg/gxg_augmented.rb, line 3266 def process(&block) result = self.clone result.iterative(&block) result end
process!(&block)
click to toggle source
# File lib/gxg/gxg_augmented.rb, line 3272 def process!(&block) self.iterative(&block) self end
search(&block)
click to toggle source
# File lib/gxg/gxg_augmented.rb, line 3277 def search(&block) results = [] if block.respond_to?(:call) results = self.iterative(&block) end results end