Skip to content

Commit

Permalink
Added unflattening, fixed short name bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mikbe committed Jan 8, 2011
1 parent 1f119b0 commit 7860c9f
Show file tree
Hide file tree
Showing 17 changed files with 1,005 additions and 538 deletions.
15 changes: 5 additions & 10 deletions Gemfile
@@ -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
27 changes: 13 additions & 14 deletions Gemfile.lock
Expand Up @@ -9,33 +9,32 @@ GEM
json (~> 1.4.6)
term-ansicolor (~> 1.0.5)
diff-lcs (1.1.2)
gherkin (2.3.2)
gherkin (2.3.3)
json (~> 1.4.6)
term-ansicolor (~> 1.0.5)
git (1.2.5)
jeweler (1.5.1)
jeweler (1.5.2)
bundler (~> 1.0.0)
git (>= 1.2.5)
rake
json (1.4.6)
rake (0.8.7)
rcov (0.9.9)
rspec (2.2.0)
rspec-core (~> 2.2)
rspec-expectations (~> 2.2)
rspec-mocks (~> 2.2)
rspec-core (2.2.1)
rspec-expectations (2.2.0)
rspec (2.4.0)
rspec-core (~> 2.4.0)
rspec-expectations (~> 2.4.0)
rspec-mocks (~> 2.4.0)
rspec-core (2.4.0)
rspec-expectations (2.4.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.2.0)
rspec-mocks (2.4.0)
term-ansicolor (1.0.5)

PLATFORMS
ruby

DEPENDENCIES
bundler (>= 1.0.0)
cucumber (>= 0.10.0)
jeweler (>= 1.5.0.pre5)
bundler
cucumber
jeweler
rcov
rspec (>= 2.2.0)
rspec
13 changes: 11 additions & 2 deletions README.markdown
Expand Up @@ -21,11 +21,20 @@ found = @hm.where {@switch == "-x" && @parameter\_type == String}

## Usage

Coming soon...

For now take a look at the spec files to see simple examples of how to use the HashModel

## Version History

0.2.0
* Fixed bug if first field name is shorter version of another field name, e.g. :short then :shorter would cause an error.
* Added unflattening records and adding unflattened records.
* Changed field separator to double underscores (to allow unflattening)
* Removed namespace module, it was annoying. Now just instantiate it with HashModel.new instead of MikBe::HashModel.new
* Now allows a single hash, instead of an array of hashes, when creating with HashModel.new(:raw_data => hash)

0.1.1 Moved to new RubyGems account

0.1.0 Initial publish

== Contributing to hash\_model

Expand Down
11 changes: 3 additions & 8 deletions Rakefile
Expand Up @@ -9,7 +9,7 @@ rescue Bundler::BundlerError => e
end
require 'rake'
require './lib/hash_model/version'
version = MikBe::HashModel::VERSION::STRING
version = HashModel::VERSION::STRING

require 'jeweler'
Jeweler::Tasks.new do |gem|
Expand All @@ -25,19 +25,14 @@ Jeweler::RubygemsDotOrgTasks.new

require 'rspec/core'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |spec|
RSpec::Core::RakeTask.new(:rspec) do |spec|
spec.pattern = FileList['spec/**/*_spec.rb']
end

RSpec::Core::RakeTask.new(:rcov) do |spec|
spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true
end

require 'cucumber/rake/task'
Cucumber::Rake::Task.new(:features)

task :default => :spec
task :default => :rspec

require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
Expand Down
169 changes: 169 additions & 0 deletions _brainstorm/hash_test.rb
@@ -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
24 changes: 24 additions & 0 deletions _brainstorm/instance_vars.rb
@@ -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
16 changes: 16 additions & 0 deletions _brainstorm/ref_val.rb
@@ -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}"
46 changes: 46 additions & 0 deletions _brainstorm/spliting.rb
@@ -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"
}

0 comments on commit 7860c9f

Please sign in to comment.