Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unflattening, fixed short name bug
- Loading branch information
Showing
17 changed files
with
1,005 additions
and
538 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 |
---|---|---|
@@ -1,14 +1,9 @@ | ||
source "http://rubygems.org" | ||
# Add dependencies required to use your gem here. | ||
# Example: | ||
# gem "activesupport", ">= 2.3.5" | ||
|
||
# Add dependencies to develop your gem here. | ||
# Include everything needed to run rake, tests, features, etc. | ||
group :development do | ||
gem "rspec", ">= 2.2.0" | ||
gem "cucumber", ">= 0.10.0" | ||
gem "bundler", ">= 1.0.0" | ||
gem "jeweler", ">= 1.5.0.pre5" | ||
gem "rcov", ">= 0" | ||
gem "rspec" | ||
gem "cucumber" | ||
gem "bundler" | ||
gem "jeweler" | ||
gem "rcov" | ||
end |
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
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,169 @@ | ||
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), "/../lib")) | ||
require 'hash_model' | ||
|
||
hash = { | ||
:field1 => "f", | ||
:field2 => { | ||
:field3 => "f3", | ||
:field4 => { | ||
:field5 => "f5", | ||
:field6 => ["f6", "f7"] | ||
} | ||
} | ||
} | ||
=begin | ||
hash2 = { | ||
:switch => ["-x", "--xtend"], | ||
:parameter => {:type => String, :require => true}, | ||
:description => "Xish stuff", | ||
:field => {:field2 => {:field3 => "ff3", :field4 => "ff4"}} | ||
} | ||
hash2 = { | ||
:switch => ["-x", "--xtend"], | ||
:parameter => {:type => String, :require => true}, | ||
:description => "Xish stuff", | ||
:field => {:field2 => [:field3 => "ff3", :field4 => "ff4", "ff5"]} | ||
} | ||
hm = HashModel.new | ||
hm << hash2 | ||
hm.flatten_index = :field__field2 | ||
flat = { | ||
:field2__field4__field5=>"f5", | ||
:field1=>"f", | ||
:field3=>"f3", | ||
:field2__field4__field6=>["f6", "f7"] | ||
} | ||
[ | ||
{ | ||
:field__field2__field3=>"ff3", | ||
:field__field2__field4=>"ff4", | ||
:switch=>["-x", "--xtend"], | ||
:parameter__type=>String, | ||
:parameter__require=>true, | ||
:description=>"Xish stuff", | ||
} | ||
] | ||
puts "\nhm: #{hm}" | ||
#hash2 = {:field => "field", :field2__field3 => "field3", :field2__field4 => {:field5 => "field5", :field6 => ["field6", "field7"]}}} | ||
=end | ||
|
||
|
||
|
||
def unflatten(input) | ||
# Seriously in need of a refactor, just looking at this hurts my brain | ||
case input | ||
when Hash | ||
new_record = {} | ||
input.each do |key, value| | ||
puts "#{key} => #{value}" | ||
# recursively look for flattened keys | ||
keys = key.to_s.split("__", 2) | ||
if keys[1] | ||
key = keys[0].to_sym | ||
value = unflatten({keys[1].to_sym => value}) | ||
end | ||
|
||
# Don't overwrite existing value | ||
if (existing = new_record[key]) | ||
# convert to array and search for subkeys if appropriate | ||
if existing.class == Hash | ||
# Convert to an array if something other than a hash is added | ||
unless value.class == Hash | ||
new_record[key] = hash_to_array(existing) | ||
new_record[key] << value | ||
else | ||
# Search subkeys for duplicate values if it's a hash | ||
unless (found_keys = existing.keys & value.keys).empty? | ||
found_keys.each do |found_key| | ||
if new_record[key][found_key].class == Hash | ||
unless value[found_key].class == Hash | ||
new_record[key] = hash_to_array(new_record[key][found_key]) | ||
new_record[key] << value[found_key] | ||
else | ||
new_record[key][found_key].merge!(value[found_key]) | ||
end | ||
end | ||
end | ||
else | ||
new_record[key].merge!(value) | ||
end | ||
end | ||
else | ||
new_record[key] << value | ||
end | ||
else | ||
new_record.merge!(key => value) | ||
end | ||
end | ||
new_record | ||
when Array | ||
# recurse into array | ||
input.collect! {|item| unflatten(item) } | ||
else | ||
input | ||
end | ||
end | ||
|
||
|
||
def hash_to_array(hash) | ||
array = [] | ||
hash.each do |key, value| | ||
array << {key => value} | ||
end | ||
array | ||
end | ||
|
||
hash = [ | ||
{ | ||
:field__field2__field3=>"ff3", | ||
:field__field2__field4=>"ff4" | ||
} | ||
] | ||
|
||
hash2 = { | ||
:switch=>["-x", "--xtend"], | ||
:parameter__type=>String, | ||
:parameter__require=>true, | ||
:description=>"Xish stuff", | ||
} | ||
|
||
|
||
hash3 = { | ||
:switch=>[{:deep1 => "deepOne"}, {:deep2 => "deepTwo"}, "--xtend"], | ||
:parameter__type=>String, | ||
:parameter__require=>true, | ||
:description=>"Xish stuff", | ||
} | ||
|
||
|
||
hash4 = { | ||
:parameter__type=>String, | ||
:switch__deep1__deep3 => "deepTwo", | ||
:parameter__type__ruby=>true, | ||
:parameter => "glorp", | ||
:parameter__require=>true, | ||
:switch__deep2 => "deepTwo", | ||
:description=>"Xish stuff", | ||
:switch => "--xtend", | ||
} | ||
|
||
|
||
unflat = unflatten(hash4) | ||
puts "\nUnflat: #{unflat}" | ||
|
||
=begin | ||
puts "to_a: #{hash2.to_a}" | ||
x = [1,2,3] | ||
y = [3,4,5] | ||
puts "\nx & y = #{x & y}" | ||
=end |
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,24 @@ | ||
class Foo | ||
|
||
attr_accessor :bar | ||
|
||
def initialize | ||
@bar = 1 | ||
end | ||
|
||
def show | ||
vars = instance_variables | ||
instance_variables.each do |var| | ||
puts "var: #{var}" | ||
end | ||
print "no vars " | ||
puts instance_variables.class | ||
end | ||
|
||
end | ||
|
||
f = Foo.new | ||
|
||
f.show | ||
|
||
puts f.instance_variables |
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,16 @@ | ||
|
||
def test_it(y) | ||
y.concat("_mod") | ||
end | ||
|
||
x = "test" | ||
test_it x | ||
puts "x id: #{x.object_id}" | ||
puts "x: #{x}" | ||
|
||
z = "z" | ||
puts "z id: #{z.object_id}" | ||
puts "z: #{z}" | ||
z += "_mod" | ||
puts "z id: #{z.object_id}" | ||
puts "z: #{z}" |
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,46 @@ | ||
|
||
def deflatten(input) | ||
case input | ||
when Hash | ||
new_hash = {} | ||
input.each do |key,value| | ||
split_key = key.to_s.split("__",2) | ||
new_hash_key = split_key[0].to_sym | ||
if split_key.length > 1 | ||
child_hash = {split_key[1].to_sym => value} | ||
value = deflatten(child_hash) | ||
end | ||
|
||
#look for existing keys so we don't overwrite them | ||
existing_value = new_hash[new_hash_key] | ||
if !existing_value.nil? | ||
if existing_value.class == Hash && value.class == Hash | ||
value = existing_value.merge(value) | ||
elsif existing_value.class == Array | ||
value = existing_value << value | ||
else | ||
value = [value, existing_value] | ||
end | ||
end | ||
new_hash.merge!(new_hash_key => value) | ||
end | ||
new_hash | ||
when Array | ||
input.collect { |value| deflatten(value.clone)} | ||
else | ||
input | ||
end # case | ||
end | ||
|
||
hash = {:switch=>"-x", :parameter__type=>String, :parameter__required=>true, :some__value__blrop=>[1,2,3], :some__hash=>{:blah=>"bloo", :bleep=>4}, :some__value=>"something", :some__others=>"others", :some__array=> [1,2,3], :description=>"the x paramemter"} | ||
|
||
puts "" | ||
puts "" | ||
puts "build_hash: #{deflatten(hash)}" | ||
|
||
{ | ||
:switch=>"-x", | ||
:parameter=>{:required=>true, :type=>String}, | ||
:some=>{:array=>[1, 2, 3], :others=>"others", :value=>[{:blrop=>[1,2,3]}, "something"], :hash=>{:blah=>"bloo", :bleep=>4}}, | ||
:description=>"the x paramemter" | ||
} |
Oops, something went wrong.