Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Hash searches fixed
- Loading branch information
Showing
7 changed files
with
286 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,5 @@ pkg | |
.DS_Store | ||
*.tmproj | ||
tmtags | ||
vendor | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), "/../lib")) | ||
require 'hash_model' | ||
=begin | ||
# Creates an object with instance variables for each field at every level | ||
# This allows using a block like {:field1==true && :field2_subfield21="potato"} | ||
def create_object_from_flat_hash(record, hash_record=Class.new.new, parent_key=nil) | ||
# Iterate through the record creating the object recursively | ||
case record | ||
when Hash | ||
record.each do |key, value| | ||
flat_key = "#{parent_key}#{"__" if !parent_key.nil?}#{key}" | ||
hash_record.instance_variable_set("@#{flat_key}", value) | ||
hash_record = create_object_from_flat_hash(value, hash_record, flat_key) | ||
end | ||
when Array | ||
record.each do |value| | ||
hash_record = create_object_from_flat_hash(value, hash_record, parent_key) | ||
end | ||
else | ||
hash_record.instance_variable_set("@#{parent_key}", record) | ||
end # case | ||
hash_record | ||
end # create_object_from_flat_hash | ||
records = [ | ||
{:switch => ["-x", "--xtended"], :parameter => {:type => String, :required => true}, :description => "Xish stuff", :something => 4}, | ||
{:switch => ["-y", "--why"], :description => "lucky what?"}, | ||
{:switch => "-z", :parameter => {:type => String, :required => true}, :description => "zee svitch zu moost calz", :something => 4}, | ||
] | ||
hm = HashModel.new(:raw_data=>records) | ||
where = hm.where {:something == 4 && :parameter__type == String && :parameter__required == true} | ||
puts "\nWhere:" | ||
puts where | ||
records = [ | ||
{:switch => ["-x", "--xtended"], :parameter => {:type => String, :required => true}, :description => "Xish stuff", :something => 4}, | ||
{:switch => ["-y", "--why"], :description => "lucky what?", :something => 7}, | ||
{:switch => "-z", :parameter => {:type => Integer, :required => true}, :description => "zee svitch zu moost calz", :something => 4}, | ||
] | ||
hm = HashModel.new(:raw_data=>records) | ||
where = hm.where {:parameter == {:type => String, :required => true}} | ||
puts "\nWhere:" | ||
puts where | ||
puts "\nflat object" | ||
flat = create_object_from_flat_hash(hm[0]) | ||
puts flat.inspect | ||
=end | ||
|
||
puts "\nproc" | ||
xproc = proc {:parameter == {:type => String, :required => true} && :switch == ["-x", "--xtended"]} | ||
xproc_source = xproc.to_source | ||
puts "xproc: #{xproc_source}" | ||
|
||
matches = xproc_source.scan(/(\:\S+) ==/) | ||
|
||
puts "matches: #{matches}" | ||
puts "\nshow items" | ||
matches.each do |item| | ||
puts "item: #{item}" | ||
end | ||
puts 'done' | ||
#"proc { #{@filter} }.call".gsub(":", "@") | ||
|
||
#x = "{:parameter == {:type => String, :required => true}, " | ||
#x.match | ||
|
||
puts "\nMatch test" | ||
text = "The future Ruby is Ruby" | ||
m1 = text.scan(/(Ruby)/) | ||
puts "m1: #{m1}" | ||
|
||
puts "\n\ndone" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
string = "My phone number is (123) 555-1234." | ||
phone_re = /\((\d{3})\)\s+(\d{3})-(\d{4})/ | ||
m = phone_re.match(string) | ||
unless m | ||
puts "There was no match..." | ||
exit | ||
end | ||
print "The whole string we started with: " | ||
puts m.string | ||
print "The entire part of the string that matched: " | ||
puts m[0] | ||
puts "The three captures: " | ||
m.captures.each do |cap| | ||
puts "Capture ##{cap}" | ||
end | ||
puts "Here's another way to get at the first capture:" | ||
print "Capture #1: " | ||
puts m[1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.