walky 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ Gemfile.lock
2
+ .rspec
3
+ .rvmrc
4
+ autotest/
5
+ *.swp
6
+ *.gem
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ script: rspec
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,44 @@
1
+ ## Walky - Simple lib to walky through easily hashes
2
+
3
+ [![Build
4
+ Status](https://secure.travis-ci.org/jhonnyquest/walky.png?branch=master)](http://travis-ci.org/jhonnyquest/walky)
5
+
6
+
7
+ Walky try create a simple way to access hashes with many keys easily.
8
+ You can move through hashes and also access others hashes with the _same path_
9
+
10
+ Example:
11
+
12
+ ```ruby
13
+ @hash = { "menu" => {
14
+ "header" => {
15
+ "screen" => "LCD",
16
+ "meme" => "Like a boss"
17
+ }
18
+ }
19
+ ```
20
+
21
+ If we have an hash like above, you can use Walky like this:
22
+
23
+ ```ruby
24
+ @walky = Walky::Walker.new(@hash)
25
+
26
+ @walky["menu header screen"] # => "LCD"
27
+ # Or:
28
+ @walky.walk("menu header screen") # => "LCD"
29
+ ```
30
+
31
+ If you want to access multiple hashes that have the same keys that the first, you can use
32
+ <tt>Walky#same_path</tt>. Example using with the code above:
33
+
34
+ ```ruby
35
+ @other = { "menu" => { "header" => { "screen" => "LED" , "meme" => "Poker face" } } }
36
+ @more_one = { "menu" => { "header" => { "screen" => "PLASM", "meme" => "LOL" } } }
37
+
38
+ # Acessing multiple hashes with one hash key
39
+ @walky["menu header"].same_path(@other, @more_one).all do |a, b, c|
40
+ a["screen"] # => "LCD"
41
+ b["screen"] # => "LED"
42
+ c["screen"] # => "PLASM"
43
+ end
44
+ ```
@@ -0,0 +1,2 @@
1
+ run: rspec
2
+ project: git://github.com/jhonnyquest/walky.git
@@ -0,0 +1,44 @@
1
+ require 'walky/version'
2
+ require 'walky/parser'
3
+ require 'walky/walk_methods'
4
+ require 'walky/instance_methods'
5
+
6
+ module Walky
7
+ def self.move(hash, path)
8
+ Walky::Parser.parse(hash, path)
9
+ end
10
+
11
+ def self.extract(hash, path)
12
+ Walky::Parser.extract(hash, path)
13
+ end
14
+
15
+ def self.extract_with_sym(hash, path)
16
+ Walky::Parser.extract_with_sym(hash, path)
17
+ end
18
+
19
+ class Walker
20
+ def initialize(hash)
21
+ @hash = hash
22
+ end
23
+
24
+ def [](path)
25
+ parse(path)
26
+ end
27
+
28
+ def walk(path)
29
+ parse(path)
30
+ end
31
+
32
+ def parse(path)
33
+ Walker.parse(@hash, path)
34
+ end
35
+
36
+ def self.parse(hash, path)
37
+ Walky::Parser.parse(hash, path)
38
+ end
39
+
40
+ def extract(path)
41
+ Walky::Parser.extract(@hash, path)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,15 @@
1
+ module Walky
2
+ module InstanceMethods
3
+ attr_accessor :walky_path
4
+
5
+ def same_path(*other)
6
+ [].tap do | ary |
7
+ ary << self
8
+ other.each do |o|
9
+ ary << Walker.parse(o, @walky_path)
10
+ end
11
+ ary.extend WalkMethods
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,36 @@
1
+ module Walky
2
+ module Parser
3
+ def self.parse(hash, path)
4
+ paths = path.split(" ")
5
+ return_hash = hash
6
+ paths.each do |pathy|
7
+ if pathy =~ /^:/
8
+ return_hash = return_hash[pathy.gsub(":", "").to_sym]
9
+ else
10
+ return_hash = return_hash[pathy]
11
+ end
12
+ end
13
+
14
+ return_hash.extend InstanceMethods
15
+ return_hash.walky_path = path
16
+
17
+ return_hash
18
+ end
19
+
20
+ def self.extract(hash, path)
21
+ Walky.move(hash, path)
22
+ end
23
+
24
+ def self.extract_with_sym(hash, path)
25
+ symbolize_keys(extract(hash, path))
26
+ end
27
+
28
+ def self.symbolize_keys(hash)
29
+ {}.tap do |h|
30
+ hash.each do |key, value|
31
+ h[key.to_sym] = value
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module Walky
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ module Walky
2
+ module WalkMethods
3
+ def all(&block)
4
+ if block_given?
5
+ yield(self)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1 @@
1
+ require 'test_notifier/runner/rspec'
@@ -0,0 +1,138 @@
1
+ require 'walky'
2
+
3
+ describe Walky do
4
+
5
+ before do
6
+ @hash = {"menu"=>{"header"=> {"screen"=>"LCD", "meme" => "Like a boss"}}}
7
+ @other = {"menu"=>{"header"=> {"screen"=>"LED", "meme" => "Poker face"}}}
8
+ @more_one = {"menu"=>{"header"=> {"screen"=>"PLASM", "meme" => "LOL"}}}
9
+ @menu_items = {
10
+ "menu" => {
11
+ "items" => [
12
+ {"item" => "Movie", "cat" => { "keywords" => ["movie", "stories"]}},
13
+ {"item" => "Shop", "cat" => {"keywords" => ["buy", "bussiness"]}},
14
+ {"item" => "Stadium", "cat" => {"keywords" => ["joy", "watch", "fun"]}},
15
+ ]
16
+ }
17
+ }
18
+
19
+ @collection = Walky::Walker.new(@menu_items)
20
+ @walky = Walky::Walker.new(@hash)
21
+ end
22
+
23
+ describe "Parse to walk" do
24
+ it "shoul have Walky#[] method" do
25
+ @walky.should respond_to(:[])
26
+ end
27
+
28
+ it "should parse a simple path" do
29
+ @walky["menu"].should be_kind_of(Hash)
30
+ end
31
+
32
+ it "should parse a two level path" do
33
+ @walky["menu header"].should be_kind_of(Hash)
34
+ end
35
+
36
+ it "should parse a last level path" do
37
+ @walky["menu header screen"].should == "LCD"
38
+ end
39
+
40
+ it "should parse by :[] or :walk" do
41
+ @walky["menu header screen"].should == "LCD"
42
+ @walky.walk("menu header screen").should == "LCD"
43
+ end
44
+ end
45
+
46
+ describe "Walk through hash" do
47
+ it "should access other with same path" do
48
+ @walky["menu header"].same_path(@other).should be_kind_of(Array)
49
+ @walky["menu header"].same_path(@other)[0].should == @walky["menu header"]
50
+ @walky["menu header"].same_path(@other)[1].should == @other["menu"]["header"]
51
+ end
52
+
53
+ it "should access multiple other hashes with same path" do
54
+ walked = @walky["menu header"].same_path(@other, @more_one)
55
+ walked.size.should == 3
56
+ walked[0].should == @walky["menu header"]
57
+ walked[1].should == @other["menu"]["header"]
58
+ walked[2].should == @more_one["menu"]["header"]
59
+ end
60
+
61
+ it "should take all sub hashes with same path" do
62
+ walked = @walky["menu header"].same_path(@other, @more_one).all do |a, b, c|
63
+ a["screen"].should == "LCD"
64
+ b["screen"].should == "LED"
65
+ c["screen"].should == "PLASM"
66
+
67
+ "#{a["screen"]}#{b["screen"]}#{c["screen"]}"
68
+ end
69
+ walked.should == "LCDLEDPLASM"
70
+ end
71
+
72
+ it "should iterate between hashes collection" do
73
+ @collection["menu items"].each do |item|
74
+ item["item"].should_not be_nil
75
+ end
76
+ end
77
+
78
+ it "should be able to navigate trough an hash" do
79
+ @collection["menu items"].each do |item|
80
+ Walky.move(item, "cat keywords").should be_kind_of(Array)
81
+ end
82
+
83
+ Walky.move(@collection["menu items"].first, "cat keywords").size.should == 2
84
+ end
85
+
86
+ it 'should walky with symbol keys' do
87
+ command = {
88
+ :type => "ls",
89
+ :params => ["l", "a"]
90
+ }
91
+ Walky.move(command, ":type").should == "ls"
92
+ Walky.move(command, ":params").should == ["l", "a"]
93
+ end
94
+
95
+ it 'should walky with symbols and string keys' do
96
+ print = {
97
+ "file" => {
98
+ :type => "A4",
99
+ :pages => [1, 2, 3],
100
+ "password" => "secret"
101
+ }
102
+ }
103
+ Walky.move(print, "file :type").should == "A4"
104
+ Walky.move(print, "file password").should == "secret"
105
+ end
106
+
107
+ it "should access keys from a sub key" do
108
+ print = {
109
+ "file" => {
110
+ :type => "A4",
111
+ :pages => [1, 2, 3],
112
+ "password" => "secret"
113
+ }
114
+ }
115
+ keys = Walky.extract(print, "file")
116
+ keys[:type].should == "A4"
117
+ keys[:pages].should == [1, 2, 3]
118
+ keys["password"].should == "secret"
119
+
120
+ extracted = @walky.extract("menu header")
121
+ extracted["screen"] = "LCD"
122
+ end
123
+
124
+ it "should access keys from a sub key and symbolize" do
125
+ print = {
126
+ "file" => {
127
+ :type => "A4",
128
+ :pages => [1, 2, 3],
129
+ "password" => "secret"
130
+ }
131
+ }
132
+
133
+ keys = Walky.extract_with_sym(print, "file")
134
+ keys[:type].should == "A4"
135
+ keys[:password].should == "secret"
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "walky/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "walky"
7
+ s.version = Walky::VERSION
8
+ s.authors = ["Jonathan Duarte"]
9
+ s.email = ["jonathan.duarte@rocketmail.com"]
10
+ s.homepage = "http://www.jonathanduarte.com.br"
11
+ s.summary = "Walky through hashes"
12
+ s.description = "Simple way to access hashes with nested keys"
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec}/*`.split("\n")
15
+
16
+ s.add_development_dependency "rspec"
17
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: walky
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jonathan Duarte
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &74795770 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *74795770
25
+ description: Simple way to access hashes with nested keys
26
+ email:
27
+ - jonathan.duarte@rocketmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - .travis.yml
34
+ - Gemfile
35
+ - Gemfile.lock
36
+ - README.md
37
+ - kronix.yml
38
+ - lib/walky.rb
39
+ - lib/walky/instance_methods.rb
40
+ - lib/walky/parser.rb
41
+ - lib/walky/version.rb
42
+ - lib/walky/walk_methods.rb
43
+ - spec/spec_helper.rb
44
+ - spec/walky_spec.rb
45
+ - walky.gemspec
46
+ homepage: http://www.jonathanduarte.com.br
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.10
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Walky through hashes
70
+ test_files: []