gimlet 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 62584aecfa34ddfae8151092011b21032b345bb9
4
- data.tar.gz: fdee5e353d0b18e4c8a39f0e809c0a573be57cd7
3
+ metadata.gz: cf39d44503a768fe66812c45cc20df9454d5b214
4
+ data.tar.gz: 10f512cf0ce4ac85fcb1273a60b2b8740757282e
5
5
  SHA512:
6
- metadata.gz: 3cb283f71b7a4a3d8487ab5949967e45cbd01f1b4beb4d642fff0a185b35b067ba8c965454b9c731ab75e6827b17e94f2ca8381093413dbeeb8dd0820dd76c13
7
- data.tar.gz: f9ab49e424779ec4479866064dd6b3bd2488bc1a53e40d8138b84aa3dcd06fec75fc854030c0f07783fc268c7d3047dd3df1f46217fda95e7f4d7deff8b66345
6
+ metadata.gz: e0c58e6c97ed992514af5640418bd67f4b77a53de3fcb91809deeeff607ed53051f04b18fc29c9711a5b3fab21c882f3aaf2661971aa952772dbd8a01d1e41ec
7
+ data.tar.gz: ccf512ff0308030fb1753bfdf54136f7c75e0de00c6daf8cb8921b6efe91ab542f15d51b0d0279becbdc56ee1e7d98e01f3496d1026925064dfbc14d09ebf62f
@@ -0,0 +1,2 @@
1
+ rvm:
2
+ - '2.0.0'
data/README.md CHANGED
@@ -25,8 +25,8 @@ Or install it yourself as:
25
25
  Put `data/people/homuhomu.yaml`,
26
26
 
27
27
  ---
28
- first_name: Akemi
29
- last_name: Homura
28
+ first_name: Homura
29
+ last_name: Akemi
30
30
 
31
31
  Then you can access the data:
32
32
 
@@ -34,9 +34,9 @@ Then you can access the data:
34
34
 
35
35
  data = Gimlet::DataStore.new('data')
36
36
 
37
- p data.to_h #=> {"people"=>{"homuhomu"=>{"first_name"=>"Akemi", "last_name"=>"Homura"}}}
38
- p data.people.homuhomu #=> {"first_name"=>"Akemi", "last_name"=>"Homura"}
39
- p data.people[:homuhomu].last_name #=> "Homura"
37
+ p data.to_h #=> {"people"=>{"homuhomu"=>{"first_name"=>"Homura", "last_name"=>"Akemi"}}}
38
+ p data.people.homuhomu #=> {"first_name"=>"Homura", "last_name"=>"Akemi"}
39
+ p data.people[:homuhomu].first_name #=> "Homura"
40
40
 
41
41
 
42
42
  ## Contributing
@@ -7,18 +7,30 @@ module Gimlet
7
7
  class DataStore
8
8
  extend Forwardable
9
9
 
10
- def initialize(data_directory)
11
- @data_directory = Pathname(data_directory)
10
+ class SourceNotFound < StandardError; end
11
+
12
+ def initialize(source_path)
13
+ @source_path = Pathname(source_path)
12
14
  @local_data = ::Gimlet::Util.recursively_enhance({})
13
15
  load_all!
14
16
  end
15
17
 
16
18
  def load_all!
17
- Pathname.glob(@data_directory + '**/*.{yaml,yml}').each do |path|
19
+ if @source_path.directory?
20
+ load_from_directory!
21
+ elsif @source_path.file?
22
+ load_from_file!
23
+ else
24
+ raise SourceNotFound, 'No such file or directory - %s' % @source_path
25
+ end
26
+ end
27
+
28
+ def load_from_directory!
29
+ Pathname.glob(@source_path + '**/*.{yaml,yml}').each do |path|
18
30
  extension = path.extname
19
31
  basename = path.basename(extension)
20
32
 
21
- parts = path.relative_path_from(@data_directory).split.map(&:to_s)[0..-1]
33
+ parts = path.relative_path_from(@source_path).split.map(&:to_s)[0..-1]
22
34
  parts.delete('.')
23
35
  parts.pop
24
36
 
@@ -28,10 +40,18 @@ module Gimlet
28
40
  current = current[part]
29
41
  end
30
42
 
31
- current[basename.to_s] = ::Gimlet::Util.recursively_enhance(YAML.load_file(path))
43
+ current[basename.to_s] = load_from_file(path)
32
44
  end
33
45
  end
34
46
 
47
+ def load_from_file!
48
+ @local_data = load_from_file(@source_path)
49
+ end
50
+
51
+ def load_from_file(path)
52
+ ::Gimlet::Util.recursively_enhance(YAML.load_file(path))
53
+ end
54
+
35
55
  def_delegators :@local_data, :[], :method_missing, :to_h, :each
36
56
  end
37
57
  end
@@ -1,3 +1,3 @@
1
1
  module Gimlet
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -71,4 +71,20 @@ describe Gimlet::DataStore do
71
71
  expect(subject.to_h).to eq({'yaml' => {'name' => 'yaml'}, 'yml' => {'name' => 'yml'}})
72
72
  end
73
73
  end
74
+
75
+ describe 'filename' do
76
+ subject { Gimlet::DataStore.new(fixture_path('yaml/hello.yaml')) }
77
+
78
+ it do
79
+ expect(subject.to_h).to eq({'message' => 'Hello world'})
80
+ end
81
+ end
82
+
83
+ describe 'not found' do
84
+ subject { Gimlet::DataStore.new('NOT_FOUND') }
85
+
86
+ it do
87
+ expect(->{ subject }).to raise_error(Gimlet::DataStore::SourceNotFound)
88
+ end
89
+ end
74
90
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gimlet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoji SHIDARA
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-01 00:00:00.000000000 Z
11
+ date: 2013-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -75,6 +75,7 @@ extra_rdoc_files: []
75
75
  files:
76
76
  - .gitignore
77
77
  - .rspec
78
+ - .travis.yml
78
79
  - Gemfile
79
80
  - LICENSE.txt
80
81
  - README.md