berg 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1db42bed4f1d7e59d44ca2eb2a154202d1ae1023
4
+ data.tar.gz: 3b8a595b4e9fda38c3a447aad6bbf09a40fa1a9c
5
+ SHA512:
6
+ metadata.gz: 0d03491c493610fd9093e7e254f5c410005adb1c431302021748aead7df817a00702f6073a886fa5594359d2fc8f8cb789380bf298059c649a835db653bdf094
7
+ data.tar.gz: b96f97617248045fa7b6d52f9b2884e7f912758d0cbae4c8fc59dbd013e50cd83435da8553cea6b34a7342df1c00fcd0306351dc4b71ceba0cfdcdadd65104cf
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in berg.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Linus Oleander
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ [![Build Status](https://travis-ci.org/oleander/berg-rb.svg?branch=master)](https://travis-ci.org/oleander/berg-rb)
2
+
3
+ # Berg
4
+
5
+ Find paths and values in complex Ruby hashes – much like css selectors.
6
+
7
+ ## Install
8
+
9
+ ```
10
+ $ gem install berg
11
+ ```
12
+
13
+ ## Example
14
+
15
+ ``` ruby
16
+ require "berg"
17
+
18
+ data = { ... }
19
+ query = ".data[1].modules[2].content[-1].title.name"
20
+ value = "My value"
21
+
22
+ # Get query for #{item} in #{data}
23
+ result1 = Berg::Key.locate(data) do |leaf|
24
+ leaf.to_s.include?(value)
25
+ end
26
+
27
+ # Get value from #{query}
28
+ result2 = Berg::Value.locate(data, query)
29
+ ```
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'berg/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "berg"
8
+ spec.version = Berg::VERSION
9
+ spec.authors = ["Linus Oleander"]
10
+ spec.email = ["linus@oleander.nu"]
11
+
12
+ spec.summary = "Find paths and values in complex Ruby hashes – much like css selectors"
13
+ spec.description = "Find paths and values in complex Ruby hashes – much like css selectors"
14
+ spec.homepage = "http://github.com/oleander/berg"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.12"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "minitest", "~> 5.0"
23
+ spec.add_development_dependency "minitest-reporters"
24
+ end
@@ -0,0 +1,8 @@
1
+ require "berg/version"
2
+ require "berg/value"
3
+ require "berg/key"
4
+ require "berg/container"
5
+ require "berg/errors"
6
+
7
+ module Berg
8
+ end
@@ -0,0 +1,10 @@
1
+ module Berg
2
+ class Found < Struct.new(:value)
3
+ def found?; true; end
4
+ end
5
+
6
+ class NotFound
7
+ def found?; false; end
8
+ def value; nil end
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ module Berg
2
+ class NothingFound < StandardError
3
+ end
4
+ end
@@ -0,0 +1,36 @@
1
+ module Berg
2
+ class Key
3
+ def initialize(&block)
4
+ @block = block
5
+ end
6
+
7
+ def self.locate(object, &block)
8
+ new(&block).locate(object, "")
9
+ end
10
+
11
+ def locate(object, current)
12
+ case object
13
+ when Array
14
+ object.each_with_index do |rest, index|
15
+ if (result = locate(rest, current + "[#{index}]")).found?
16
+ return result
17
+ end
18
+ end
19
+ when Hash
20
+ object.each_pair do |key, value|
21
+ if (result = locate(value, current + ".#{key}")).found?
22
+ return result
23
+ end
24
+ end
25
+ when TrueClass, FalseClass, NilClass, Fixnum, String
26
+ return @block.call(object) ? Found.new(current) : NotFound.new
27
+ when Found, NotFound
28
+ return object
29
+ else
30
+ raise "Type #{object.class} not supported"
31
+ end
32
+
33
+ return NotFound.new
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,47 @@
1
+ module Berg
2
+ class Value
3
+ ARRAY_ID = /\[(\d+)\]/
4
+
5
+ def initialize(object, query, debug = false)
6
+ @object = object
7
+ @query = query
8
+ @debug = debug
9
+ end
10
+
11
+ def self.locate(object, query, debug = false)
12
+ new(object, query, debug).locate
13
+ end
14
+
15
+ def locate
16
+ result, _ = keys.inject([@object, ""]) do |(latest, trace), key|
17
+ found = case key
18
+ when ARRAY_ID
19
+ latest[$1.to_i]
20
+ else
21
+ latest[key] || latest[key.to_sym]
22
+ end
23
+
24
+ trace = add(trace, key)
25
+
26
+ unless found
27
+ fail "#{latest.class} doesn't contain key '#{key}' in '#{trace}'" if @debug
28
+ return NotFound.new unless found
29
+ end
30
+
31
+ [found, trace]
32
+ end
33
+
34
+ return Found.new(result)
35
+ end
36
+
37
+ private
38
+
39
+ def keys
40
+ @query.split(/\.|(\[\d+\])/).reject(&:empty?)
41
+ end
42
+
43
+ def add(trace, key)
44
+ trace << (key.match(ARRAY_ID) ? key : ".#{key}")
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module Berg
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: berg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Linus Oleander
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-reporters
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Find paths and values in complex Ruby hashes – much like css selectors
70
+ email:
71
+ - linus@oleander.nu
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - berg.gemspec
83
+ - lib/berg.rb
84
+ - lib/berg/container.rb
85
+ - lib/berg/errors.rb
86
+ - lib/berg/key.rb
87
+ - lib/berg/value.rb
88
+ - lib/berg/version.rb
89
+ homepage: http://github.com/oleander/berg
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.5.1
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Find paths and values in complex Ruby hashes – much like css selectors
113
+ test_files: []