hashmodel 0.4.0.beta1 → 0.4.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/LICENSE.txt +1 -1
- data/README.markdown +65 -27
- data/hashmodel.gemspec +1 -1
- data/lib/hash_model/hash_model.rb +13 -105
- data/lib/hash_model/hash_model_create_object.rb +62 -0
- data/lib/hash_model/hash_model_delete.rb +13 -0
- data/lib/hash_model/hash_model_filter.rb +37 -0
- data/lib/hash_model/hash_model_flatten.rb +47 -0
- data/lib/hash_model/hash_model_group.rb +18 -0
- data/lib/hash_model/hash_model_update.rb +118 -0
- data/lib/hash_model/hash_model_where.rb +27 -0
- data/lib/hash_model/version.rb +1 -1
- data/lib/hashmodel.rb +1 -0
- data/lib/monkey_patch/deep_clone.rb +27 -0
- data/spec/hash_model/_current_spec.rb +6 -0
- data/spec/hash_model/hash_model_adding_records_spec.rb +338 -0
- data/spec/hash_model/hash_model_array_methods_spec.rb +132 -0
- data/spec/hash_model/hash_model_comparisons_spec.rb +55 -0
- data/spec/hash_model/hash_model_delete_spec.rb +51 -0
- data/spec/hash_model/hash_model_flattening_spec.rb +92 -0
- data/spec/hash_model/hash_model_group_spec.rb +67 -0
- data/spec/hash_model/hash_model_searching_spec.rb +245 -0
- data/spec/hash_model/hash_model_spec.rb +1 -1
- data/spec/hash_model/hash_model_unflattening_spec.rb +34 -0
- data/spec/hash_model/hash_model_update_spec.rb +147 -0
- data/spec/hash_model/hash_model_where_spec.rb +287 -0
- data/spec/support/configuration.rb +50 -0
- data/spec/support/debug_print.rb +13 -0
- data/spec/support/proc_tester.rb +17 -0
- metadata +49 -27
- data/_brainstorm/StrangeMarshal.txt +0 -0
- data/_brainstorm/_readme +0 -1
- data/_brainstorm/block_wheres.rb +0 -80
- data/_brainstorm/clone.rb +0 -19
- data/_brainstorm/hash_model_examples.rb +0 -57
- data/_brainstorm/hash_test.rb +0 -169
- data/_brainstorm/inspect.rb +0 -26
- data/_brainstorm/instance_vars.rb +0 -24
- data/_brainstorm/proc_tests.rb +0 -14
- data/_brainstorm/ref_val.rb +0 -16
- data/_brainstorm/regex_captures.rb +0 -18
- data/_brainstorm/spliting.rb +0 -46
- data/_brainstorm/test.rb +0 -27
- data/_brainstorm/unflat.rb +0 -16
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'hashmodel'
|
2
|
+
|
3
|
+
RSpec.configure do |config|
|
4
|
+
config.before(:each) do
|
5
|
+
|
6
|
+
@records = [
|
7
|
+
{:switch => ["-x", "--xtended"], :parameter => {:type => String, :required => true}, :description => "Xish stuff"},
|
8
|
+
{:switch => ["-y", "--why"], :description => "lucky what?"},
|
9
|
+
{:switch => "-z", :parameter => {:type => String}, :description => "zee svitch zu moost calz"},
|
10
|
+
]
|
11
|
+
@hm = HashModel.new(:raw_data=>@records)
|
12
|
+
|
13
|
+
@records2 = [
|
14
|
+
{:switch => ["-p", "--pea"], :parameter => {:type => Hash, :required => false}, :description => "Pea soup"},
|
15
|
+
{:switch => ["-q", "--quit"], :description => "exit the game"},
|
16
|
+
{:switch => "-r", :parameter => {:type => Fixnum}, :description => "Arrrrrrrrrrgh!"},
|
17
|
+
]
|
18
|
+
@hm2 = HashModel.new(:raw_data=>@records2)
|
19
|
+
|
20
|
+
@flat_records = [
|
21
|
+
{:switch=>"-x", :parameter=>{:type=>String, :required=>true}, :description=>"Xish stuff", :_id=>0, :_group_id=>0},
|
22
|
+
{:switch=>"--xtended", :parameter=>{:type=>String, :required=>true}, :description=>"Xish stuff", :_id=>1, :_group_id=>0},
|
23
|
+
{:switch=>"-y", :description=>"lucky what?", :_id=>2, :_group_id=>1},
|
24
|
+
{:switch=>"--why", :description=>"lucky what?", :_id=>3, :_group_id=>1},
|
25
|
+
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :_id=>4, :_group_id=>2}
|
26
|
+
]
|
27
|
+
|
28
|
+
@flat_records2 =[
|
29
|
+
{:switch=>"-p", :parameter=>{:type=>Hash, :required=>false}, :description=>"Pea soup", :_id=>0, :_group_id=>0},
|
30
|
+
{:switch=>"--pea", :parameter=>{:type=>Hash, :required=>false}, :description=>"Pea soup", :_id=>1, :_group_id=>0},
|
31
|
+
{:switch=>"-q", :description=>"exit the game", :_id=>2, :_group_id=>1},
|
32
|
+
{:switch=>"--quit", :description=>"exit the game", :_id=>3, :_group_id=>1},
|
33
|
+
{:switch=>"-r", :parameter=>{:type=>Fixnum}, :description=>"Arrrrrrrrrrgh!", :_id=>4, :_group_id=>2}
|
34
|
+
]
|
35
|
+
|
36
|
+
@flat_records_all = [
|
37
|
+
{:switch=>"-x", :parameter=>{:type=>String, :required=>true}, :description=>"Xish stuff", :_id=>0, :_group_id=>0},
|
38
|
+
{:switch=>"--xtended", :parameter=>{:type=>String, :required=>true}, :description=>"Xish stuff", :_id=>1, :_group_id=>0},
|
39
|
+
{:switch=>"-y", :description=>"lucky what?", :_id=>2, :_group_id=>1},
|
40
|
+
{:switch=>"--why", :description=>"lucky what?", :_id=>3, :_group_id=>1},
|
41
|
+
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :_id=>4, :_group_id=>2},
|
42
|
+
{:switch=>"-p", :parameter=>{:type=>Hash, :required=>false}, :description=>"Pea soup", :_id=>5, :_group_id=>3},
|
43
|
+
{:switch=>"--pea", :parameter=>{:type=>Hash, :required=>false}, :description=>"Pea soup", :_id=>6, :_group_id=>3},
|
44
|
+
{:switch=>"-q", :description=>"exit the game", :_id=>7, :_group_id=>4},
|
45
|
+
{:switch=>"--quit", :description=>"exit the game", :_id=>8, :_group_id=>4},
|
46
|
+
{:switch=>"-r", :parameter=>{:type=>Fixnum}, :description=>"Arrrrrrrrrrgh!", :_id=>9, :_group_id=>5}
|
47
|
+
]
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Make a new class with the given name and create instance variables set using key,value pairs.
|
2
|
+
# e.g. new_class = create_class "Dummy", {:x=>"x", :y=>String, :z=>["1",2,:three]}
|
3
|
+
def create_proc_tester(property_value_hash)
|
4
|
+
proc_test = Class.new.new
|
5
|
+
|
6
|
+
# Add an evaluator method to make it more clear what we are doing
|
7
|
+
proc_test.class.class_eval do
|
8
|
+
define_method(:xql?) { |&block| instance_eval &block }
|
9
|
+
end
|
10
|
+
|
11
|
+
# Add the property values to this instance of the class
|
12
|
+
property_value_hash.each do |key, value|
|
13
|
+
proc_test.instance_variable_set("@#{key}", value)
|
14
|
+
end
|
15
|
+
|
16
|
+
proc_test
|
17
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashmodel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.0.
|
4
|
+
version: 0.4.0.beta2
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,12 +9,12 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-03-
|
12
|
+
date: 2011-03-21 00:00:00.000000000 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: sourcify
|
17
|
-
requirement: &
|
17
|
+
requirement: &2158452320 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: '0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *2158452320
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: file-tail
|
28
|
-
requirement: &
|
28
|
+
requirement: &2158451900 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ! '>='
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: '0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *2158451900
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: rspec
|
39
|
-
requirement: &
|
39
|
+
requirement: &2158451480 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ! '>='
|
@@ -44,10 +44,10 @@ dependencies:
|
|
44
44
|
version: '0'
|
45
45
|
type: :development
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *2158451480
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: cucumber
|
50
|
-
requirement: &
|
50
|
+
requirement: &2158451040 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
53
53
|
- - ! '>='
|
@@ -55,10 +55,10 @@ dependencies:
|
|
55
55
|
version: '0'
|
56
56
|
type: :development
|
57
57
|
prerelease: false
|
58
|
-
version_requirements: *
|
59
|
-
description: A
|
60
|
-
|
61
|
-
and
|
58
|
+
version_requirements: *2158451040
|
59
|
+
description: A hash based MVC model class that makes searching and updating deeply
|
60
|
+
nested hashes a breeze. You can store deeply nested hashes and still easily flatten,
|
61
|
+
query, and update the records using flattened field names.
|
62
62
|
email:
|
63
63
|
- mikbe.tk@gmail.com
|
64
64
|
executables: []
|
@@ -73,20 +73,6 @@ files:
|
|
73
73
|
- LICENSE.txt
|
74
74
|
- README.markdown
|
75
75
|
- Rakefile
|
76
|
-
- _brainstorm/StrangeMarshal.txt
|
77
|
-
- _brainstorm/_readme
|
78
|
-
- _brainstorm/block_wheres.rb
|
79
|
-
- _brainstorm/clone.rb
|
80
|
-
- _brainstorm/hash_model_examples.rb
|
81
|
-
- _brainstorm/hash_test.rb
|
82
|
-
- _brainstorm/inspect.rb
|
83
|
-
- _brainstorm/instance_vars.rb
|
84
|
-
- _brainstorm/proc_tests.rb
|
85
|
-
- _brainstorm/ref_val.rb
|
86
|
-
- _brainstorm/regex_captures.rb
|
87
|
-
- _brainstorm/spliting.rb
|
88
|
-
- _brainstorm/test.rb
|
89
|
-
- _brainstorm/unflat.rb
|
90
76
|
- autotest/discover.rb
|
91
77
|
- features/README
|
92
78
|
- features/hash_model_flatten.feature
|
@@ -98,12 +84,34 @@ files:
|
|
98
84
|
- hashmodel.gemspec
|
99
85
|
- lib/hash_model/exceptions.rb
|
100
86
|
- lib/hash_model/hash_model.rb
|
87
|
+
- lib/hash_model/hash_model_create_object.rb
|
88
|
+
- lib/hash_model/hash_model_delete.rb
|
89
|
+
- lib/hash_model/hash_model_filter.rb
|
90
|
+
- lib/hash_model/hash_model_flatten.rb
|
91
|
+
- lib/hash_model/hash_model_group.rb
|
92
|
+
- lib/hash_model/hash_model_update.rb
|
93
|
+
- lib/hash_model/hash_model_where.rb
|
101
94
|
- lib/hash_model/version.rb
|
102
95
|
- lib/hashmodel.rb
|
96
|
+
- lib/monkey_patch/deep_clone.rb
|
97
|
+
- spec/hash_model/_current_spec.rb
|
103
98
|
- spec/hash_model/_marshelling_spec.rb
|
99
|
+
- spec/hash_model/hash_model_adding_records_spec.rb
|
100
|
+
- spec/hash_model/hash_model_array_methods_spec.rb
|
101
|
+
- spec/hash_model/hash_model_comparisons_spec.rb
|
102
|
+
- spec/hash_model/hash_model_delete_spec.rb
|
103
|
+
- spec/hash_model/hash_model_flattening_spec.rb
|
104
|
+
- spec/hash_model/hash_model_group_spec.rb
|
105
|
+
- spec/hash_model/hash_model_searching_spec.rb
|
104
106
|
- spec/hash_model/hash_model_spec.rb
|
107
|
+
- spec/hash_model/hash_model_unflattening_spec.rb
|
108
|
+
- spec/hash_model/hash_model_update_spec.rb
|
109
|
+
- spec/hash_model/hash_model_where_spec.rb
|
105
110
|
- spec/hash_model/hashmodel.dat
|
106
111
|
- spec/spec_helper.rb
|
112
|
+
- spec/support/configuration.rb
|
113
|
+
- spec/support/debug_print.rb
|
114
|
+
- spec/support/proc_tester.rb
|
107
115
|
has_rdoc: true
|
108
116
|
homepage: http://github.com/mikbe/hashmodel
|
109
117
|
licenses: []
|
@@ -137,7 +145,21 @@ test_files:
|
|
137
145
|
- features/step_definitions/hash_model_steps.rb
|
138
146
|
- features/support/env.rb
|
139
147
|
- features/support/helper.rb
|
148
|
+
- spec/hash_model/_current_spec.rb
|
140
149
|
- spec/hash_model/_marshelling_spec.rb
|
150
|
+
- spec/hash_model/hash_model_adding_records_spec.rb
|
151
|
+
- spec/hash_model/hash_model_array_methods_spec.rb
|
152
|
+
- spec/hash_model/hash_model_comparisons_spec.rb
|
153
|
+
- spec/hash_model/hash_model_delete_spec.rb
|
154
|
+
- spec/hash_model/hash_model_flattening_spec.rb
|
155
|
+
- spec/hash_model/hash_model_group_spec.rb
|
156
|
+
- spec/hash_model/hash_model_searching_spec.rb
|
141
157
|
- spec/hash_model/hash_model_spec.rb
|
158
|
+
- spec/hash_model/hash_model_unflattening_spec.rb
|
159
|
+
- spec/hash_model/hash_model_update_spec.rb
|
160
|
+
- spec/hash_model/hash_model_where_spec.rb
|
142
161
|
- spec/hash_model/hashmodel.dat
|
143
162
|
- spec/spec_helper.rb
|
163
|
+
- spec/support/configuration.rb
|
164
|
+
- spec/support/debug_print.rb
|
165
|
+
- spec/support/proc_tester.rb
|
File without changes
|
data/_brainstorm/_readme
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
This is my scratchpad area where I try stuff out. Nothing really useful here.
|
data/_brainstorm/block_wheres.rb
DELETED
@@ -1,80 +0,0 @@
|
|
1
|
-
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), "/../lib"))
|
2
|
-
require 'hash_model'
|
3
|
-
=begin
|
4
|
-
|
5
|
-
# Creates an object with instance variables for each field at every level
|
6
|
-
# This allows using a block like {:field1==true && :field2_subfield21="potato"}
|
7
|
-
def create_object_from_flat_hash(record, hash_record=Class.new.new, parent_key=nil)
|
8
|
-
|
9
|
-
# Iterate through the record creating the object recursively
|
10
|
-
case record
|
11
|
-
when Hash
|
12
|
-
record.each do |key, value|
|
13
|
-
flat_key = "#{parent_key}#{"__" if !parent_key.nil?}#{key}"
|
14
|
-
hash_record.instance_variable_set("@#{flat_key}", value)
|
15
|
-
hash_record = create_object_from_flat_hash(value, hash_record, flat_key)
|
16
|
-
end
|
17
|
-
when Array
|
18
|
-
record.each do |value|
|
19
|
-
hash_record = create_object_from_flat_hash(value, hash_record, parent_key)
|
20
|
-
end
|
21
|
-
else
|
22
|
-
hash_record.instance_variable_set("@#{parent_key}", record)
|
23
|
-
end # case
|
24
|
-
|
25
|
-
hash_record
|
26
|
-
end # create_object_from_flat_hash
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
records = [
|
31
|
-
{:switch => ["-x", "--xtended"], :parameter => {:type => String, :required => true}, :description => "Xish stuff", :something => 4},
|
32
|
-
{:switch => ["-y", "--why"], :description => "lucky what?"},
|
33
|
-
{:switch => "-z", :parameter => {:type => String, :required => true}, :description => "zee svitch zu moost calz", :something => 4},
|
34
|
-
]
|
35
|
-
hm = HashModel.new(:raw_data=>records)
|
36
|
-
where = hm.where {:something == 4 && :parameter__type == String && :parameter__required == true}
|
37
|
-
puts "\nWhere:"
|
38
|
-
puts where
|
39
|
-
|
40
|
-
|
41
|
-
records = [
|
42
|
-
{:switch => ["-x", "--xtended"], :parameter => {:type => String, :required => true}, :description => "Xish stuff", :something => 4},
|
43
|
-
{:switch => ["-y", "--why"], :description => "lucky what?", :something => 7},
|
44
|
-
{:switch => "-z", :parameter => {:type => Integer, :required => true}, :description => "zee svitch zu moost calz", :something => 4},
|
45
|
-
]
|
46
|
-
hm = HashModel.new(:raw_data=>records)
|
47
|
-
where = hm.where {:parameter == {:type => String, :required => true}}
|
48
|
-
puts "\nWhere:"
|
49
|
-
puts where
|
50
|
-
|
51
|
-
|
52
|
-
puts "\nflat object"
|
53
|
-
flat = create_object_from_flat_hash(hm[0])
|
54
|
-
puts flat.inspect
|
55
|
-
=end
|
56
|
-
|
57
|
-
puts "\nproc"
|
58
|
-
xproc = proc {:parameter == {:type => String, :required => true} && :switch == ["-x", "--xtended"]}
|
59
|
-
xproc_source = xproc.to_source
|
60
|
-
puts "xproc: #{xproc_source}"
|
61
|
-
|
62
|
-
matches = xproc_source.scan(/(\:\S+) ==/)
|
63
|
-
|
64
|
-
puts "matches: #{matches}"
|
65
|
-
puts "\nshow items"
|
66
|
-
matches.each do |item|
|
67
|
-
puts "item: #{item}"
|
68
|
-
end
|
69
|
-
puts 'done'
|
70
|
-
#"proc { #{@filter} }.call".gsub(":", "@")
|
71
|
-
|
72
|
-
#x = "{:parameter == {:type => String, :required => true}, "
|
73
|
-
#x.match
|
74
|
-
|
75
|
-
puts "\nMatch test"
|
76
|
-
text = "The future Ruby is Ruby"
|
77
|
-
m1 = text.scan(/(Ruby)/)
|
78
|
-
puts "m1: #{m1}"
|
79
|
-
|
80
|
-
puts "\n\ndone"
|
data/_brainstorm/clone.rb
DELETED
@@ -1,57 +0,0 @@
|
|
1
|
-
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), "/../lib"))
|
2
|
-
require 'hash_model'
|
3
|
-
require 'sourcify'
|
4
|
-
|
5
|
-
records = [
|
6
|
-
{:switch => ["-x", "--xtended"], :parameter => {:type => String, :require => true}, :description => "Xish stuff"},
|
7
|
-
{:switch => ["-y", "--why"], :description => "lucky what?"},
|
8
|
-
{:switch => "-z", :parameter => {:type => String}, :description => "zee svitch zu moost calz"},
|
9
|
-
]
|
10
|
-
|
11
|
-
=begin
|
12
|
-
hash_model = HashModel.new(:raw_data=>records)
|
13
|
-
|
14
|
-
records = [
|
15
|
-
{:switch => ["-x", "--xtended"], :parameter => {:type => String, :require => true}, :description => "Xish stuff"},
|
16
|
-
{:switch => ["-y", "--why"], :description => "lucky what?"}
|
17
|
-
]
|
18
|
-
|
19
|
-
records2 = {:switch => "-z", :parameter => {:type => String}, :description => "zee svitch zu moost calz"}
|
20
|
-
|
21
|
-
hash_model = HashModel.new(:raw_data => records)
|
22
|
-
hash_model2 = HashModel.new(:raw_data => records2)
|
23
|
-
|
24
|
-
hash_model << hash_model2
|
25
|
-
# or
|
26
|
-
hash_model += hash_model2
|
27
|
-
|
28
|
-
|
29
|
-
# Flatten index is automatically set to the first field ever given
|
30
|
-
# but you can change it
|
31
|
-
records = [
|
32
|
-
{:switch => ["-x", "--xtended"], :parameter => {:type => String, :require => true}, :description => "Xish stuff"},
|
33
|
-
{:switch => ["-y", "--why"], :description => "lucky what?"},
|
34
|
-
{:switch => "-z", :parameter => {:type => String}, :description => "zee svitch zu moost calz"},
|
35
|
-
]
|
36
|
-
hash_model = HashModel.new(:raw_data=>records)
|
37
|
-
|
38
|
-
puts hash_model.flatten_index
|
39
|
-
# you can use flattened field names
|
40
|
-
hash_model.flatten_index = :parameter__type
|
41
|
-
puts hash_model
|
42
|
-
|
43
|
-
>> {:parameter__type=>String, :switch=>["-x", "--xtended"], :parameter__require=>true, :description=>"Xish stuff", :_id=>0, :_group_id=>0}
|
44
|
-
>> {:parameter__type=>nil, :switch=>["-y", "--why"], :description=>"lucky what?", :_id=>1, :_group_id=>1}
|
45
|
-
>> {:parameter__type=>String, :switch=>"-z", :description=>"zee svitch zu moost calz", :_id=>2, :_group_id=>2}
|
46
|
-
|
47
|
-
# Notice that records that don't have the flatten index have their value set to nil
|
48
|
-
=end
|
49
|
-
# The real strength of the class is its ability to search flattened fields
|
50
|
-
hash_model = HashModel.new(:raw_data=>records)
|
51
|
-
|
52
|
-
# default is to use the flatten index
|
53
|
-
puts hash_model.where("-z")
|
54
|
-
|
55
|
-
# but you can also use a block with boolean search parameters
|
56
|
-
# You must write the field names as @variables instead of symbols
|
57
|
-
puts hash_model.where {@parameter_type}
|
data/_brainstorm/hash_test.rb
DELETED
@@ -1,169 +0,0 @@
|
|
1
|
-
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), "/../lib"))
|
2
|
-
require 'hash_model'
|
3
|
-
|
4
|
-
hash = {
|
5
|
-
:field1 => "f",
|
6
|
-
:field2 => {
|
7
|
-
:field3 => "f3",
|
8
|
-
:field4 => {
|
9
|
-
:field5 => "f5",
|
10
|
-
:field6 => ["f6", "f7"]
|
11
|
-
}
|
12
|
-
}
|
13
|
-
}
|
14
|
-
=begin
|
15
|
-
|
16
|
-
hash2 = {
|
17
|
-
:switch => ["-x", "--xtend"],
|
18
|
-
:parameter => {:type => String, :require => true},
|
19
|
-
:description => "Xish stuff",
|
20
|
-
:field => {:field2 => {:field3 => "ff3", :field4 => "ff4"}}
|
21
|
-
}
|
22
|
-
|
23
|
-
hash2 = {
|
24
|
-
:switch => ["-x", "--xtend"],
|
25
|
-
:parameter => {:type => String, :require => true},
|
26
|
-
:description => "Xish stuff",
|
27
|
-
:field => {:field2 => [:field3 => "ff3", :field4 => "ff4", "ff5"]}
|
28
|
-
}
|
29
|
-
|
30
|
-
|
31
|
-
hm = HashModel.new
|
32
|
-
hm << hash2
|
33
|
-
hm.flatten_index = :field__field2
|
34
|
-
|
35
|
-
|
36
|
-
flat = {
|
37
|
-
:field2__field4__field5=>"f5",
|
38
|
-
:field1=>"f",
|
39
|
-
:field3=>"f3",
|
40
|
-
:field2__field4__field6=>["f6", "f7"]
|
41
|
-
}
|
42
|
-
|
43
|
-
[
|
44
|
-
{
|
45
|
-
:field__field2__field3=>"ff3",
|
46
|
-
:field__field2__field4=>"ff4",
|
47
|
-
:switch=>["-x", "--xtend"],
|
48
|
-
:parameter__type=>String,
|
49
|
-
:parameter__require=>true,
|
50
|
-
:description=>"Xish stuff",
|
51
|
-
}
|
52
|
-
]
|
53
|
-
|
54
|
-
puts "\nhm: #{hm}"
|
55
|
-
#hash2 = {:field => "field", :field2__field3 => "field3", :field2__field4 => {:field5 => "field5", :field6 => ["field6", "field7"]}}}
|
56
|
-
=end
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
def unflatten(input)
|
61
|
-
# Seriously in need of a refactor, just looking at this hurts my brain
|
62
|
-
case input
|
63
|
-
when Hash
|
64
|
-
new_record = {}
|
65
|
-
input.each do |key, value|
|
66
|
-
puts "#{key} => #{value}"
|
67
|
-
# recursively look for flattened keys
|
68
|
-
keys = key.to_s.split("__", 2)
|
69
|
-
if keys[1]
|
70
|
-
key = keys[0].to_sym
|
71
|
-
value = unflatten({keys[1].to_sym => value})
|
72
|
-
end
|
73
|
-
|
74
|
-
# Don't overwrite existing value
|
75
|
-
if (existing = new_record[key])
|
76
|
-
# convert to array and search for subkeys if appropriate
|
77
|
-
if existing.class == Hash
|
78
|
-
# Convert to an array if something other than a hash is added
|
79
|
-
unless value.class == Hash
|
80
|
-
new_record[key] = hash_to_array(existing)
|
81
|
-
new_record[key] << value
|
82
|
-
else
|
83
|
-
# Search subkeys for duplicate values if it's a hash
|
84
|
-
unless (found_keys = existing.keys & value.keys).empty?
|
85
|
-
found_keys.each do |found_key|
|
86
|
-
if new_record[key][found_key].class == Hash
|
87
|
-
unless value[found_key].class == Hash
|
88
|
-
new_record[key] = hash_to_array(new_record[key][found_key])
|
89
|
-
new_record[key] << value[found_key]
|
90
|
-
else
|
91
|
-
new_record[key][found_key].merge!(value[found_key])
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
95
|
-
else
|
96
|
-
new_record[key].merge!(value)
|
97
|
-
end
|
98
|
-
end
|
99
|
-
else
|
100
|
-
new_record[key] << value
|
101
|
-
end
|
102
|
-
else
|
103
|
-
new_record.merge!(key => value)
|
104
|
-
end
|
105
|
-
end
|
106
|
-
new_record
|
107
|
-
when Array
|
108
|
-
# recurse into array
|
109
|
-
input.collect! {|item| unflatten(item) }
|
110
|
-
else
|
111
|
-
input
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
|
116
|
-
def hash_to_array(hash)
|
117
|
-
array = []
|
118
|
-
hash.each do |key, value|
|
119
|
-
array << {key => value}
|
120
|
-
end
|
121
|
-
array
|
122
|
-
end
|
123
|
-
|
124
|
-
hash = [
|
125
|
-
{
|
126
|
-
:field__field2__field3=>"ff3",
|
127
|
-
:field__field2__field4=>"ff4"
|
128
|
-
}
|
129
|
-
]
|
130
|
-
|
131
|
-
hash2 = {
|
132
|
-
:switch=>["-x", "--xtend"],
|
133
|
-
:parameter__type=>String,
|
134
|
-
:parameter__require=>true,
|
135
|
-
:description=>"Xish stuff",
|
136
|
-
}
|
137
|
-
|
138
|
-
|
139
|
-
hash3 = {
|
140
|
-
:switch=>[{:deep1 => "deepOne"}, {:deep2 => "deepTwo"}, "--xtend"],
|
141
|
-
:parameter__type=>String,
|
142
|
-
:parameter__require=>true,
|
143
|
-
:description=>"Xish stuff",
|
144
|
-
}
|
145
|
-
|
146
|
-
|
147
|
-
hash4 = {
|
148
|
-
:parameter__type=>String,
|
149
|
-
:switch__deep1__deep3 => "deepTwo",
|
150
|
-
:parameter__type__ruby=>true,
|
151
|
-
:parameter => "glorp",
|
152
|
-
:parameter__require=>true,
|
153
|
-
:switch__deep2 => "deepTwo",
|
154
|
-
:description=>"Xish stuff",
|
155
|
-
:switch => "--xtend",
|
156
|
-
}
|
157
|
-
|
158
|
-
|
159
|
-
unflat = unflatten(hash4)
|
160
|
-
puts "\nUnflat: #{unflat}"
|
161
|
-
|
162
|
-
=begin
|
163
|
-
puts "to_a: #{hash2.to_a}"
|
164
|
-
|
165
|
-
x = [1,2,3]
|
166
|
-
y = [3,4,5]
|
167
|
-
|
168
|
-
puts "\nx & y = #{x & y}"
|
169
|
-
=end
|