data_depo 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 40075cd33feb0c2bd83eeacebacc019a0258960a
4
- data.tar.gz: bdd519dfd952ea9686ea83364675a6749ccce72c
3
+ metadata.gz: bd02a9bfaa21f3a7e33f71d9f3ea41104f61d4bc
4
+ data.tar.gz: 3e78b7f459050c4f40e2df4489a59465e9d3c16a
5
5
  SHA512:
6
- metadata.gz: 32f923b8ee6ae7392a19eb5a95a9a575049fbfe0859d4c29a5a33b9f6a0f846d026a154477e566a7a19957564bb875304341c33f6f64a705f36ff6563e7bcffb
7
- data.tar.gz: afd550417815024a44c548a2bdb97b903e66b4f634f3ad1f69b5f002a8aa71d414463e3d2870925378eb27e594836418322b378af96af46741e47c181e84359f
6
+ metadata.gz: ff21242222621c3145f933f3806b9f3ded6ba82720a3ca6828a491a5d41473a276eb72f7516bd636deca670739ca4a8ad35bb47f7e2738c68cf64973f06928d3
7
+ data.tar.gz: 12676ece7801289b0fd7d4421ab2b2653f0bfe43638cab28e641ee344423be4cc6c0d3d0f9224ec30410c71e2c3c586e83af246109743038ba781d4696b334d7
data/README.md CHANGED
@@ -2,10 +2,16 @@
2
2
 
3
3
  `DataDepo` is data depository utility for testing, etc.
4
4
 
5
+ ## Installation
6
+
7
+ ```
8
+ $ gem install data_depo
9
+ ```
10
+
5
11
  ## Basic Usage
6
12
 
7
13
  When you write the following data file in YAML file
8
- as `deops/users.yml`...
14
+ as `depots/users.yml`...
9
15
 
10
16
  ```
11
17
  - name: John Doe
@@ -24,7 +30,7 @@ you can access the contents the following manner:
24
30
  ```ruby
25
31
  require 'data_depo'
26
32
 
27
- DataDepo.definition_path = 'depos'
33
+ DataDepo.definition_path = 'depots'
28
34
  DataDepo.users.each do |u|
29
35
  puts u["name"]
30
36
  end
@@ -37,7 +43,7 @@ end
37
43
  You can have multiple data files as one group.
38
44
  In case you have the following two YAML files:
39
45
 
40
- (`depos/users/foo.yml`)
46
+ (`depots/users/foo.yml`)
41
47
  ```
42
48
  - name: John Doe
43
49
  email: john.doe@xxx.xx
@@ -47,7 +53,7 @@ In case you have the following two YAML files:
47
53
  password: drowssap
48
54
  ```
49
55
 
50
- (`depos/users/baa.yml`)
56
+ (`depots/users/baa.yml`)
51
57
  ```
52
58
  - name: John Roe
53
59
  email: john.roe@xxx.xx
@@ -62,7 +68,7 @@ You can access contents of the both files in the same manner.
62
68
  ```ruby
63
69
  require 'data_depo'
64
70
 
65
- DataDepo.definition_path = 'depos'
71
+ DataDepo.definition_path = 'depots'
66
72
  DataDepo.users.each do |u|
67
73
  puts u["name"]
68
74
  end
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
10
10
  s.homepage = "https://github.com/hosim/data_depo"
11
11
  s.files = `git ls-files`.split("\n")
12
12
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
- s.version = "0.0.1"
13
+ s.version = "0.0.2"
14
14
  s.license = "MIT"
15
15
 
16
16
  s.add_development_dependency 'rspec'
@@ -17,6 +17,7 @@ module DataDepo
17
17
  @loader = YAMLLoader
18
18
  end
19
19
 
20
+ private
20
21
  def method_missing(name, *args, &block)
21
22
  return super if name.to_s[-1] == '='
22
23
 
@@ -6,18 +6,22 @@ module DataDepo
6
6
  def gen(name, array)
7
7
  a = self[*array]
8
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
9
+ a.instance_variable_set(:@name, nm)
10
+ mod = action_module(nm)
11
+ (class << a; self; end).__send__(:include, mod) if mod
19
12
  a
20
13
  end
14
+
15
+ private
16
+ def action_module(name)
17
+ nm = if name.respond_to?(:camelize)
18
+ name.camelize
19
+ else
20
+ name.split('_').map {|s| s.capitalize }.join
21
+ end
22
+ nm << 'Action'
23
+ DataDepo.const_defined?(nm) ? DataDepo.const_get(nm) : nil
24
+ end
21
25
  end
22
26
  end
23
27
  end
@@ -11,6 +11,7 @@ module DataDepo
11
11
  end
12
12
 
13
13
  def load
14
+ raise NotImplementedError
14
15
  end
15
16
 
16
17
  attr_reader :paths
@@ -29,6 +29,7 @@ module DataDepo
29
29
  loader.files
30
30
  end
31
31
 
32
+ private
32
33
  def method_missing(name, *args, &block)
33
34
  loader = DataDepo.current_loader.new(@path)
34
35
  unless loader.files.empty?
@@ -0,0 +1,52 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+ require 'data_depo'
4
+
5
+ describe DataDepo::ArrayData do
6
+ describe '.gen' do
7
+ context "when passing `foo' as name" do
8
+ let(:foo) do
9
+ DataDepo::ArrayData.gen('foo', [:a, :b, :c, :d, :e])
10
+ end
11
+
12
+ it "has `foo' as an instance variable `name'" do
13
+ name = foo.instance_variable_get(:@name)
14
+ expect(name).to eq 'foo'
15
+ end
16
+
17
+ context "when DataDepo::FooAction module is defined and " \
18
+ "it has `abcde' method" do
19
+ before do
20
+ module DataDepo::FooAction
21
+ def abcde; self; end
22
+ end
23
+ end
24
+
25
+ after do
26
+ DataDepo.class_eval { remove_const(:FooAction) }
27
+ end
28
+
29
+ it "responds to `abcde'" do
30
+ expect(foo).to respond_to(:abcde)
31
+ end
32
+ end
33
+
34
+ context "when DataDepo::BaaAction module is defined and " \
35
+ "it has `vwxyz' method" do
36
+ before do
37
+ module DataDepo::BaaAction
38
+ def vwxyz; self; end
39
+ end
40
+ end
41
+
42
+ after do
43
+ DataDepo.class_eval { remove_const(:BaaAction) }
44
+ end
45
+
46
+ it "does not respond to `vwxyz'" do
47
+ expect(foo).not_to respond_to(:vwxyz)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -122,5 +122,20 @@ describe DataDepo do
122
122
  expect(path).to eq [:foo, :baa, :baz]
123
123
  end
124
124
  end
125
+
126
+ context "when a custom action is loaded" do
127
+ require "#{File.dirname(__FILE__)}/samples/animals_action"
128
+
129
+ before do
130
+ DataDepo.definition_path = "#{File.dirname(__FILE__)}/samples"
131
+ end
132
+
133
+ it "returns a value with calling the custom action" do
134
+ query = DataDepo.animals
135
+ value = query.custom_action_a
136
+ expect(value).to be_an_instance_of(Hash)
137
+ expect(value.keys).to include(:mammal)
138
+ end
139
+ end
125
140
  end
126
141
  end
@@ -0,0 +1,12 @@
1
+ - name: albatross
2
+ species: bird
3
+ - name: alligator
4
+ species: reptile
5
+ - name: alpaca
6
+ species: mammal
7
+ - name: anteater
8
+ species: mammal
9
+ - name: antelope
10
+ species: mammal
11
+ - name: armadillo
12
+ species: mammal
@@ -0,0 +1,14 @@
1
+ - name: baboon
2
+ species: mammal
3
+ - name: barracuda
4
+ species: fish
5
+ - name: bat
6
+ species: mammal
7
+ - name: bear
8
+ species: mammal
9
+ - name: beaver
10
+ species: mammal
11
+ - name: bison
12
+ species: mammal
13
+ - name: boar
14
+ species: mammal
@@ -0,0 +1,14 @@
1
+ - name: camel
2
+ species: mammal
3
+ - name: capybara
4
+ species: mammal
5
+ - name: cheetah
6
+ species: mammal
7
+ - name: chinchilla
8
+ species: mammal
9
+ - name: crane
10
+ species: bird
11
+ - name: crocodile
12
+ species: reptile
13
+ - name: crow
14
+ species: bird
@@ -0,0 +1,14 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module DataDepo
4
+ module AnimalsAction
5
+ def custom_action_a
6
+ self.each.inject({}) {|h, animal|
7
+ key = animal["species"].to_sym
8
+ h[key] ||= []
9
+ h[key] << animal["name"]
10
+ h
11
+ }
12
+ end
13
+ end
14
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_depo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - hosim
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-01 00:00:00.000000000 Z
11
+ date: 2014-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -41,12 +41,13 @@ files:
41
41
  - lib/data_depo/loader.rb
42
42
  - lib/data_depo/query.rb
43
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
44
+ - spec/data_depo/array_data_spec.rb
48
45
  - spec/data_depo/query_spec.rb
49
46
  - spec/data_depo_spec.rb
47
+ - spec/samples/animals/a.yml
48
+ - spec/samples/animals/b.yml
49
+ - spec/samples/animals/c.yml
50
+ - spec/samples/animals_action.rb
50
51
  - spec/samples/capitals/1.yml
51
52
  - spec/samples/capitals/2.yml
52
53
  - spec/samples/users.yml
@@ -76,8 +77,13 @@ signing_key:
76
77
  specification_version: 4
77
78
  summary: Data depository utility for testing, etc.
78
79
  test_files:
80
+ - spec/data_depo/array_data_spec.rb
79
81
  - spec/data_depo/query_spec.rb
80
82
  - spec/data_depo_spec.rb
83
+ - spec/samples/animals/a.yml
84
+ - spec/samples/animals/b.yml
85
+ - spec/samples/animals/c.yml
86
+ - spec/samples/animals_action.rb
81
87
  - spec/samples/capitals/1.yml
82
88
  - spec/samples/capitals/2.yml
83
89
  - spec/samples/users.yml
@@ -1,25 +0,0 @@
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
@@ -1,20 +0,0 @@
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
@@ -1,45 +0,0 @@
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
@@ -1,23 +0,0 @@
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