active_tools 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/active_tools/actionpack/action_view/alt_rendering.rb +8 -8
- data/lib/active_tools/actionpack/action_view.rb +4 -4
- data/lib/active_tools/actionpack.rb +2 -2
- data/lib/active_tools/activesupport.rb +6 -0
- data/lib/active_tools/bundle.rb +2 -0
- data/lib/active_tools/core_extension/deep_copy.rb +51 -0
- data/lib/active_tools/core_extension/deep_merge.rb +35 -0
- data/lib/active_tools/core_extension/hashup.rb +28 -0
- data/lib/active_tools/core_extension/merge_hashup.rb +28 -0
- data/lib/active_tools/core_extension.rb +30 -0
- data/lib/active_tools/railtie.rb +3 -3
- data/lib/active_tools/version.rb +1 -1
- metadata +8 -2
data/lib/active_tools/bundle.rb
CHANGED
@@ -0,0 +1,51 @@
|
|
1
|
+
module ActiveTools
|
2
|
+
module CoreExtension
|
3
|
+
|
4
|
+
module DeepCopy
|
5
|
+
# Return the 'deep' brand new copy of Hash, Array or Set. All nested hashes/arrays/sets rebuilded at the same way.
|
6
|
+
|
7
|
+
module Hash
|
8
|
+
def deep_copy(&block)
|
9
|
+
self.class.new.tap do |new_hash|
|
10
|
+
each do |k, v|
|
11
|
+
new_hash[k] = case v
|
12
|
+
when Hash, Array, Set then v.deep_copy(&block)
|
13
|
+
else
|
14
|
+
block_given? ? yield(v) : v.dup rescue v
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module Array
|
22
|
+
def deep_copy(&block)
|
23
|
+
self.class.new.tap do |new_array|
|
24
|
+
each do |v|
|
25
|
+
new_array << case v
|
26
|
+
when Hash, Array, Set then v.deep_copy(&block)
|
27
|
+
else
|
28
|
+
block_given? ? yield(v) : v.dup rescue v
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
module Set
|
36
|
+
def deep_copy(&block)
|
37
|
+
self.class.new.tap do |new_set|
|
38
|
+
each do |v|
|
39
|
+
new_set << case v
|
40
|
+
when Hash, Array, Set then v.deep_copy(&block)
|
41
|
+
else
|
42
|
+
block_given? ? yield(v) : v.dup rescue v
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module ActiveTools
|
2
|
+
module CoreExtension
|
3
|
+
|
4
|
+
module DeepMerge
|
5
|
+
|
6
|
+
module Hash
|
7
|
+
# Return the merged Hash with another +hash+, where the possible child hashes are also merged.
|
8
|
+
#
|
9
|
+
# === Example:
|
10
|
+
#
|
11
|
+
# h1 = {:breakfast => {:eggs => 2, :bread => 1}, :lunch => {:steak => 1, :salad => 1}}
|
12
|
+
# h2 = {:breakfast => {:coffee => :espresso, :juice => 1}, :lunch => {:tea => 2}, :dinner => :none}
|
13
|
+
# h1.deep_merge(h2)
|
14
|
+
# #=> {:breakfast=>{:eggs=>2, :bread=>1, :coffee=>:espresso, :juice=>1}, :lunch=>{:steak=>1, :salad=>1, :tea=>2}, :dinner=>:none}
|
15
|
+
def deep_merge(other_hash = {})
|
16
|
+
dup.tap do |hash|
|
17
|
+
other_hash.each do |key, value|
|
18
|
+
if !hash.has_key?(key) || !hash[key].is_a?(Hash)
|
19
|
+
hash[key] = value
|
20
|
+
elsif hash[key].is_a?(Hash) && value.is_a?(Hash)
|
21
|
+
hash[key].deep_merge!(value)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# .nested_merge replaces the source hash.
|
28
|
+
def deep_merge!(other_hash = {})
|
29
|
+
replace(deep_merge(other_hash))
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module ActiveTools
|
2
|
+
module CoreExtension
|
3
|
+
|
4
|
+
module Hashup
|
5
|
+
module Array
|
6
|
+
# Return a nested Hash object from Array's elements sequence, where elements used as names of +hash+ keys.
|
7
|
+
# The last element of array would be the last nested value.
|
8
|
+
#
|
9
|
+
# === Example:
|
10
|
+
#
|
11
|
+
# [:vehicle, :car, :ford, :mustang, "2 please"].hashup
|
12
|
+
#
|
13
|
+
# #=> {:vehicle=>{:car=>{:ford=>{:mustang=>"2 please"}}}}
|
14
|
+
def hashup
|
15
|
+
raise(Exception, "At least 2 elements needed!") if size < 2
|
16
|
+
value = delete_at(-1)
|
17
|
+
{}.tap do |hash|
|
18
|
+
index = 0
|
19
|
+
while index < size
|
20
|
+
hash = hash[at(index)] = (index + 1 == size) ? value : {}
|
21
|
+
index += 1
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# dependent on deep_merge and deep_copy
|
2
|
+
|
3
|
+
module ActiveTools
|
4
|
+
module CoreExtension
|
5
|
+
|
6
|
+
module MergeHashup
|
7
|
+
module Hash
|
8
|
+
# Merge hashup sequence.
|
9
|
+
#
|
10
|
+
# === Example:
|
11
|
+
#
|
12
|
+
# params = {"controller"=>"comments", "action"=>"show", "id"=>34, "article_id"=>3, "page"=>{"article"=>2}}
|
13
|
+
#
|
14
|
+
# params.merge_hashup(:page, :article, 34)
|
15
|
+
# # => {:controller => "comments", :action => "show", :id => 34, :article_id => 3, :page => {:article => 2, :comment => 34}}
|
16
|
+
#
|
17
|
+
def merge_hashup(*args)
|
18
|
+
deep_merge(args.hashup)
|
19
|
+
end
|
20
|
+
|
21
|
+
def merge_hashup!(*args)
|
22
|
+
deep_merge!(args.hashup)
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'active_tools/core_extension/deep_copy'
|
2
|
+
require 'active_tools/core_extension/deep_merge'
|
3
|
+
require 'active_tools/core_extension/hashup'
|
4
|
+
require 'active_tools/core_extension/merge_hashup'
|
5
|
+
|
6
|
+
module ActiveTools
|
7
|
+
module CoreExtension
|
8
|
+
|
9
|
+
module HashExtension
|
10
|
+
include DeepCopy::Hash
|
11
|
+
include DeepMerge::Hash
|
12
|
+
include MergeHashup::Hash
|
13
|
+
end
|
14
|
+
|
15
|
+
module ArrayExtension
|
16
|
+
include DeepCopy::Array
|
17
|
+
include Hashup::Array
|
18
|
+
end
|
19
|
+
|
20
|
+
module SetExtension
|
21
|
+
include DeepCopy::Set
|
22
|
+
end
|
23
|
+
|
24
|
+
::Hash.send(:include, HashExtension)
|
25
|
+
::Array.send(:include, ArrayExtension)
|
26
|
+
::Set.send(:include, SetExtension)
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
data/lib/active_tools/railtie.rb
CHANGED
@@ -4,11 +4,11 @@ require 'active_tools/bundle'
|
|
4
4
|
module ActiveTools
|
5
5
|
class Railtie < ::Rails::Railtie
|
6
6
|
config.before_initialize do
|
7
|
-
ActiveSupport.on_load :active_record do
|
7
|
+
::ActiveSupport.on_load :active_record do
|
8
8
|
end
|
9
|
-
ActiveSupport.on_load :action_controller do
|
9
|
+
::ActiveSupport.on_load :action_controller do
|
10
10
|
end
|
11
|
-
ActiveSupport.on_load :action_view do
|
11
|
+
::ActiveSupport.on_load :action_view do
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
data/lib/active_tools/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -44,7 +44,13 @@ files:
|
|
44
44
|
- lib/active_tools/actionpack.rb
|
45
45
|
- lib/active_tools/actionpack/action_view.rb
|
46
46
|
- lib/active_tools/actionpack/action_view/alt_rendering.rb
|
47
|
+
- lib/active_tools/activesupport.rb
|
47
48
|
- lib/active_tools/bundle.rb
|
49
|
+
- lib/active_tools/core_extension.rb
|
50
|
+
- lib/active_tools/core_extension/deep_copy.rb
|
51
|
+
- lib/active_tools/core_extension/deep_merge.rb
|
52
|
+
- lib/active_tools/core_extension/hashup.rb
|
53
|
+
- lib/active_tools/core_extension/merge_hashup.rb
|
48
54
|
- lib/active_tools/engine.rb
|
49
55
|
- lib/active_tools/railtie.rb
|
50
56
|
- lib/active_tools/version.rb
|