data_depo 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 40075cd33feb0c2bd83eeacebacc019a0258960a
4
+ data.tar.gz: bdd519dfd952ea9686ea83364675a6749ccce72c
5
+ SHA512:
6
+ metadata.gz: 32f923b8ee6ae7392a19eb5a95a9a575049fbfe0859d4c29a5a33b9f6a0f846d026a154477e566a7a19957564bb875304341c33f6f64a705f36ff6563e7bcffb
7
+ data.tar.gz: afd550417815024a44c548a2bdb97b903e66b4f634f3ad1f69b5f002a8aa71d414463e3d2870925378eb27e594836418322b378af96af46741e47c181e84359f
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 hosim <github.hosim@gmail.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # DataDepo
2
+
3
+ `DataDepo` is data depository utility for testing, etc.
4
+
5
+ ## Basic Usage
6
+
7
+ When you write the following data file in YAML file
8
+ as `deops/users.yml`...
9
+
10
+ ```
11
+ - name: John Doe
12
+ email: john.doe@xxx.xx
13
+ password: password
14
+ - name: Jane Doe
15
+ email: jane.doe@xxx.xx
16
+ password: drowssap
17
+ - name: John Smith
18
+ email: john.smith@xxx.xx
19
+ password: bassworq
20
+ ```
21
+
22
+ you can access the contents the following manner:
23
+
24
+ ```ruby
25
+ require 'data_depo'
26
+
27
+ DataDepo.definition_path = 'depos'
28
+ DataDepo.users.each do |u|
29
+ puts u["name"]
30
+ end
31
+
32
+ => John Doe
33
+ Jane Doe
34
+ John Smith
35
+ ```
36
+
37
+ You can have multiple data files as one group.
38
+ In case you have the following two YAML files:
39
+
40
+ (`depos/users/foo.yml`)
41
+ ```
42
+ - name: John Doe
43
+ email: john.doe@xxx.xx
44
+ password: password
45
+ - name: Jane Doe
46
+ email: jane.doe@xxx.xx
47
+ password: drowssap
48
+ ```
49
+
50
+ (`depos/users/baa.yml`)
51
+ ```
52
+ - name: John Roe
53
+ email: john.roe@xxx.xx
54
+ password: bassworq
55
+ - name: Jane Roe
56
+ email: jane.roe@xxx.xx
57
+ password: dassworp
58
+ ```
59
+
60
+ You can access contents of the both files in the same manner.
61
+
62
+ ```ruby
63
+ require 'data_depo'
64
+
65
+ DataDepo.definition_path = 'depos'
66
+ DataDepo.users.each do |u|
67
+ puts u["name"]
68
+ end
69
+ ```
70
+
71
+ When `DataDepo` receives a method, it searches a file or a directory
72
+ that has a name of the method in the path defined by `definition_path`
73
+ and tries to read data from there.
74
+
75
+ ## Other usage
76
+
77
+ ### Custom action
78
+
79
+ You can define some custom actions.
80
+ When you define the following module...
81
+
82
+ ```ruby
83
+ module DataDepo
84
+ module UsersAction
85
+ def puts_all
86
+ self.each do |u|
87
+ puts u
88
+ end
89
+ end
90
+ end
91
+ end
92
+ ```
93
+
94
+ you can do this.
95
+
96
+ ```ruby
97
+ DataDepo.users.puts_all
98
+ => {"name"=>"John Doe", "email"=>"john.doe@xxx.xx", "password"=>"password"}
99
+ ...
100
+ ```
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task default: :spec
data/data_depo.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ # coding: utf-8
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "data_depo"
6
+ s.authors = ["hosim"]
7
+ s.email = ["github.hosim@gmail.com"]
8
+ s.description = "DataDepo is data depository utility for testing, etc."
9
+ s.summary = "Data depository utility for testing, etc."
10
+ s.homepage = "https://github.com/hosim/data_depo"
11
+ s.files = `git ls-files`.split("\n")
12
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ s.version = "0.0.1"
14
+ s.license = "MIT"
15
+
16
+ s.add_development_dependency 'rspec'
17
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+
3
+ module DataDepo
4
+ class ArrayData < Array
5
+ class << self
6
+ def gen(name, array)
7
+ a = self[*array]
8
+ nm = name.to_s.split('/').first
9
+ mod = if nm.respond_to?(:camelize)
10
+ nm.camelize
11
+ else
12
+ nm.split('_').map {|s| s.capitalize }.join
13
+ end
14
+ mod << 'Action'
15
+ if DataDepo.const_defined?(mod)
16
+ sig = class << a; self; end
17
+ sig.__send__(:include, DataDepo.const_get(mod))
18
+ end
19
+ a
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ # coding: utf-8
2
+
3
+ module DataDepo
4
+ class Loader
5
+ def initialize(paths)
6
+ roots = ::DataDepo.definition_path
7
+ @paths = roots.map {|root|
8
+ File.join(root, *paths.map {|path| path.to_s})
9
+ }
10
+ @paths = [File.join(*paths.map {|path| path.to_s})] if @paths.empty?
11
+ end
12
+
13
+ def load
14
+ end
15
+
16
+ attr_reader :paths
17
+ end
18
+ end
@@ -0,0 +1,41 @@
1
+ # coding: utf-8
2
+
3
+ require 'data_depo/array_data'
4
+
5
+ module DataDepo
6
+ class Query
7
+ include Enumerable
8
+
9
+ def initialize(name=nil, path=[])
10
+ @name = name
11
+ @path = path
12
+ end
13
+
14
+ def [](*keys)
15
+ raise if keys.empty?
16
+
17
+ @name ||= keys.first
18
+ path = (@path || []) | keys
19
+ self.class.new(@name, path)
20
+ end
21
+
22
+ def each(&block)
23
+ data = DataDepo.current_loader.new(@path).load
24
+ data.each(&block)
25
+ end
26
+
27
+ def files
28
+ loader = DataDepo.current_loader.new(@path)
29
+ loader.files
30
+ end
31
+
32
+ def method_missing(name, *args, &block)
33
+ loader = DataDepo.current_loader.new(@path)
34
+ unless loader.files.empty?
35
+ a = ArrayData.gen(@name, loader.load)
36
+ return a.respond_to?(name) ? a.__send__(name, *args, &block) : super
37
+ end
38
+ super
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ require 'yaml'
3
+ require 'data_depo/loader'
4
+
5
+ module DataDepo
6
+ class YAMLLoader < Loader
7
+ def load
8
+ files.each.inject([]) {|a, file|
9
+ load_file_into_array(file, a); a
10
+ }
11
+ end
12
+
13
+ def files
14
+ @paths.each.inject([]) do |a, path|
15
+ Dir.glob(File.join(path, '**', '*.yml')) {|file|
16
+ a << file
17
+ } if File.directory?(path)
18
+
19
+ filename = path + '.yml'
20
+ a << filename if File.file?(filename)
21
+ a
22
+ end
23
+ end
24
+
25
+ private
26
+ def load_file_into_array(file, array)
27
+ yaml = YAML.load_file(file)
28
+ if yaml.is_a? Array
29
+ array.concat(yaml)
30
+ else
31
+ array << yaml
32
+ end
33
+ end
34
+ end
35
+ end
data/lib/data_depo.rb ADDED
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+
3
+ require 'data_depo/query'
4
+ require 'data_depo/yaml_loader'
5
+
6
+ module DataDepo
7
+ class << self
8
+ def definition_path=(paths)
9
+ @definition_paths = Array(paths)
10
+ end
11
+
12
+ def definition_path
13
+ @definition_paths
14
+ end
15
+
16
+ def current_loader
17
+ @loader = YAMLLoader
18
+ end
19
+
20
+ def method_missing(name, *args, &block)
21
+ return super if name.to_s[-1] == '='
22
+
23
+ query ||= Query.new
24
+ if name == :[]
25
+ query.__send__(name, *args, &block)
26
+ else
27
+ query = query.__send__(:[], name)
28
+ query = query[*args] unless args.empty?
29
+ query
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+
3
+ module DataPitcher
4
+ class Category
5
+ def initialize
6
+ @key_items = {}
7
+ @path_items = {}
8
+ end
9
+
10
+ def register(item, key)
11
+ hash = key.is_a?(Array) ? @path_items : @key_items
12
+ hash[key] = item
13
+ end
14
+
15
+ def has?(key)
16
+ hash = key.is_a?(Array) ? @path_items : @key_items
17
+ hash.has_key?(key)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,45 @@
1
+ # coding: utf-8
2
+
3
+ require 'yaml'
4
+ require 'data_pitcher/category'
5
+ require 'data_pitcher/loader'
6
+
7
+ module DataPitcher
8
+ class Holder
9
+ class << self
10
+ def add(path, root)
11
+ item = Loader.load(path, root)
12
+ category = (items[item.category] ||= Category.new)
13
+ category.register(item.data, item.name) unless category.has?(item.name)
14
+ category.register(item.data, item.pieces)
15
+ end
16
+
17
+ def keys
18
+ items.keys
19
+ end
20
+
21
+ def select(category)
22
+ items[category]
23
+ end
24
+
25
+ private :new
26
+ private
27
+ def items
28
+ @items ||= {}
29
+ end
30
+ end
31
+
32
+ attr_reader :category, :name, :pieces, :data
33
+
34
+ def initialize(path, root)
35
+ @path = path
36
+ pieces = path[root.length..-1].split('/')
37
+ @name = pieces.pop[0...-4]
38
+ @category = pieces.shift
39
+ @category = pieces.shift if @category and @category.empty?
40
+ @name ||= @name
41
+ @pieces = Array[*(pieces || []), @name]
42
+ @data = YAML.load_file(@path)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+
3
+ require 'yaml'
4
+
5
+ module DataPitcher
6
+ module Loader
7
+ class << self
8
+ def load(path, root)
9
+ pieces = path[root.length..-1].split('/')
10
+ name = pieces.pop[0...((File.extname(path) + 1) * -1)]
11
+ category = pieces.shift
12
+ category = pieces.shift if category and category.empty?
13
+ category ||= name
14
+ breadcrumb = Array[*(pieces || []), name]
15
+ data = YAML.load_fie(path)
16
+
17
+ DataItem.new(category, name, breadcrumb, data)
18
+ end
19
+ end
20
+
21
+ DataItem = Struct.new(:category, :name, :breadcrumb, :data)
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+
3
+ require "data_pitcher/holder"
4
+
5
+ module DataPitcher
6
+ class << self
7
+ def definiton_paths=(paths)
8
+ @definition_paths = Array(paths)
9
+ end
10
+
11
+ def load
12
+ (@definition_paths || []).uniq.each do |path|
13
+ if File.directory?(path)
14
+ Dir.glob(File.join(path, '**', '*.yml')).each do |file|
15
+ Holder.add(file, path)
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ def throw(name, key)
22
+ item = Holder.select(name.to_s)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,42 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+ require 'data_depo'
4
+
5
+ describe DataDepo::Query do
6
+ describe "#[]" do
7
+ context "when passing only one argument" do
8
+ it "returns a Query instance" do
9
+ query = described_class.new
10
+ expect(query[:foo]).to be_an_instance_of(DataDepo::Query)
11
+ end
12
+
13
+ it "returns an object that has the argument's value as 'name'" do
14
+ query = described_class.new[:foo]
15
+ name = query.instance_variable_get(:@name)
16
+ expect(name).to be :foo
17
+ end
18
+
19
+ it "returns an object that has an array of the same one as 'path'" do
20
+ query = described_class.new[:foo]
21
+ path = query.instance_variable_get(:@path)
22
+ expect(path).to eq [:foo]
23
+ end
24
+ end
25
+ end
26
+
27
+ describe "each" do
28
+ context "when a file that is specified by 'path' exists" do
29
+ before do
30
+ DataDepo.definition_path = \
31
+ File.join(File.dirname(__FILE__), '..', 'samples')
32
+ end
33
+
34
+ context "when no block is passed" do
35
+ it "returns an Enumerator instance" do
36
+ query = described_class.new[:users]
37
+ expect(query.each).to be_an_instance_of(Enumerator)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,126 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'data_depo'
5
+
6
+ describe DataDepo do
7
+ describe ".[]" do
8
+ context "when passing only one argument" do
9
+ it "returns a Query instance" do
10
+ query = DataDepo[:key]
11
+ expect(query).to be_an_instance_of(DataDepo::Query)
12
+ end
13
+
14
+ it "returns an object that has the argument's value as 'name'" do
15
+ query = DataDepo[:key]
16
+ name = query.instance_variable_get(:@name)
17
+ expect(name).to eq :key
18
+ end
19
+
20
+ it "returns an object that has an array of the same one as 'path'" do
21
+ query = DataDepo[:key]
22
+ path = query.instance_variable_get(:@path)
23
+ expect(path).to eq [:key]
24
+ end
25
+ end
26
+
27
+ context "when passing some arguments" do
28
+ it "returns a Query instance" do
29
+ query = DataDepo[:foo, :baa, :baz]
30
+ expect(query).to be_an_instance_of(DataDepo::Query)
31
+ end
32
+
33
+ it "returns an object that has a first parameters as 'name'" do
34
+ query = DataDepo[:foo, :baa, :baz]
35
+ name = query.instance_variable_get(:@name)
36
+ expect(name).to be :foo
37
+ end
38
+
39
+ it "returns an object that has all of arguments as 'path'" do
40
+ query = DataDepo[:foo, :baa, :baz]
41
+ path = query.instance_variable_get(:@path)
42
+ expect(path).to eq [:foo, :baa, :baz]
43
+ end
44
+ end
45
+
46
+ context 'when chain call' do
47
+ context 'when passing only one argument' do
48
+ it "returns a Query instance" do
49
+ query = DataDepo[:key0][:key1]
50
+ expect(query).to be_an_instance_of(DataDepo::Query)
51
+ end
52
+
53
+ it "returns an object that has first call's argument as 'name'" do
54
+ query = DataDepo[:key0][:key1]
55
+ name = query.instance_variable_get(:@name)
56
+ expect(name).to be :key0
57
+ end
58
+
59
+ it "returns an object that has all call's argument value as 'path'" do
60
+ query = DataDepo[:key0][:key1]
61
+ path = query.instance_variable_get(:@path)
62
+ expect(path).to eq [:key0, :key1]
63
+ end
64
+ end
65
+
66
+ context "when passing some arguments" do
67
+ it "returns a Query instance" do
68
+ query = DataDepo[:foo, :baa, :baz][:pee, :kaa, :boo]
69
+ expect(query).to be_an_instance_of(DataDepo::Query)
70
+ end
71
+
72
+ it "returns an object that has first call's first argument value as 'name'" do
73
+ query = DataDepo[:foo, :baa, :baz][:pee, :kaa, :boo]
74
+ name = query.instance_variable_get(:@name)
75
+ expect(name).to be :foo
76
+ end
77
+
78
+ it "returns an object that has all of arguments values as 'path'" do
79
+ query = DataDepo[:foo, :baa, :baz][:pee, :kaa, :boo]
80
+ path = query.instance_variable_get(:@path)
81
+ expect(path).to eq [:foo, :baa, :baz, :pee, :kaa, :boo]
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ describe ".method_missing" do
88
+ context "when calling without any arguments" do
89
+ it "returns a Query instance" do
90
+ query = DataDepo.foo
91
+ expect(query).to be_an_instance_of(DataDepo::Query)
92
+ end
93
+
94
+ it "returns an object that has a method name as 'name'" do
95
+ query = DataDepo.foo
96
+ name = query.instance_variable_get(:@name)
97
+ expect(name).to be :foo
98
+ end
99
+
100
+ it "returns an object that has an array of the same value as 'path'" do
101
+ query = DataDepo.foo
102
+ path = query.instance_variable_get(:@path)
103
+ expect(path).to eq [:foo]
104
+ end
105
+ end
106
+
107
+ context "when calling with some arguments" do
108
+ it "returns a Query instance" do
109
+ query = DataDepo.foo(:baa, :baz)
110
+ expect(query).to be_an_instance_of(DataDepo::Query)
111
+ end
112
+
113
+ it "returns an object that has a first argument value as 'name'" do
114
+ query = DataDepo.foo(:baa, :baz)
115
+ name = query.instance_variable_get(:@name)
116
+ expect(name).to be :foo
117
+ end
118
+
119
+ it "returns an object that has an array of a method name and all of arguments as 'path'" do
120
+ query = DataDepo.foo(:baa, :baz)
121
+ path = query.instance_variable_get(:@path)
122
+ expect(path).to eq [:foo, :baa, :baz]
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,12 @@
1
+ - state: Alabama
2
+ abr: AL
3
+ capital: Montgomery
4
+ - state: Alaska
5
+ abr: AK
6
+ capital: Juneau
7
+ - state: Arizona
8
+ abr: AZ
9
+ capital: Phoenix
10
+ - state: Arkansas
11
+ abr: AR
12
+ capital: Little Rock
@@ -0,0 +1,9 @@
1
+ - state: California
2
+ abr: CA
3
+ capital: Sacramento
4
+ - state: Colorado
5
+ abr: CO
6
+ capital: Denver
7
+ - state: Connecticut
8
+ abr: CT
9
+ capital: Hartford
@@ -0,0 +1,6 @@
1
+ - name: John Doe
2
+ email: john.doe@xxx.xx
3
+ - name: Jonnie Doe
4
+ email: jonnie.doe@xxx.xx
5
+ - name: Janie Roe
6
+ email: janie.roe@xxx.xx
@@ -0,0 +1,6 @@
1
+ # coding: utf-8
2
+ require 'rspec'
3
+ require 'data_depo'
4
+
5
+ RSpec.configure do |config|
6
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: data_depo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - hosim
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: DataDepo is data depository utility for testing, etc.
28
+ email:
29
+ - github.hosim@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - Gemfile
35
+ - MIT-LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - data_depo.gemspec
39
+ - lib/data_depo.rb
40
+ - lib/data_depo/array_data.rb
41
+ - lib/data_depo/loader.rb
42
+ - lib/data_depo/query.rb
43
+ - lib/data_depo/yaml_loader.rb
44
+ - lib/data_pitcher.rb
45
+ - lib/data_pitcher/category.rb
46
+ - lib/data_pitcher/holder.rb
47
+ - lib/data_pitcher/loader.rb
48
+ - spec/data_depo/query_spec.rb
49
+ - spec/data_depo_spec.rb
50
+ - spec/samples/capitals/1.yml
51
+ - spec/samples/capitals/2.yml
52
+ - spec/samples/users.yml
53
+ - spec/spec_helper.rb
54
+ homepage: https://github.com/hosim/data_depo
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.0.14
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Data depository utility for testing, etc.
78
+ test_files:
79
+ - spec/data_depo/query_spec.rb
80
+ - spec/data_depo_spec.rb
81
+ - spec/samples/capitals/1.yml
82
+ - spec/samples/capitals/2.yml
83
+ - spec/samples/users.yml
84
+ - spec/spec_helper.rb