cabi 0.1.0 → 0.1.1

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: 83fa2db5429675243c8acd14559770b0f65fd5f2
4
- data.tar.gz: 5fb58665d172b8e4ba324fc316fea0fc50ec2436
3
+ metadata.gz: eb182df9ed5993329f7ae5ab75021a2841c95a1d
4
+ data.tar.gz: 99cb126fd223820989b2dd16e7df542426834bd1
5
5
  SHA512:
6
- metadata.gz: b4d55e6e7c80c0c49565eeffe659866662adac9b4668faab05f11c73b315816af9902044cd313e6814b26069a3dea09727ccf85960cd2210c15104801af40b90
7
- data.tar.gz: 34b74e174b4396ec9d7073bb501549e8a98e8c0dd7eca7d92882aad6d0bc2e26b592ae87abb3449485f9859a9c05bf8feabbb1122030e1fe4fd74c28d4c9dc9a
6
+ metadata.gz: 6b1ae9a7cb08d2353d3dc25b43f59c5de4e7bc2db01aca962d65fbfaf6e527477553ea1f159cf83e6ce2b7494a120c8be838211919c699d36eef27f133426200
7
+ data.tar.gz: ca256fb320b45f733c92b2ea772ca4c78fa73e7fab9009ff71aef7cbd8e22bae285c8bc840e4e3ec913e61e2dcdb6ecc6e0d67475bfbd46fcf6e1e3cec052a3f
data/README.md CHANGED
@@ -8,7 +8,7 @@ Cabi is a flat-file datastore where data is stored by directory stucture and acc
8
8
 
9
9
  ``` bash
10
10
  $ gem install cabi
11
- $ cabi init --mock
11
+ $ cabi init --mock # use --mock to init cabi cache with some fake data
12
12
  ```
13
13
 
14
14
  Then access your data like so:
@@ -16,7 +16,7 @@ Then access your data like so:
16
16
  ```bash
17
17
  $ irb
18
18
  > require 'cabi'
19
- > Cabi::Cache.read('pages:about:body')
19
+ > Cabi.read('pages:about:body')
20
20
  => "<h1>Hello, Cabi!</h1>"
21
21
  ```
22
22
 
@@ -24,29 +24,42 @@ $ irb
24
24
 
25
25
  Assuming your `cabi-cache` folder has the following structure:
26
26
 
27
- - my-project
28
- |
29
- |--cabi-cache
30
- |-- info.yml
31
- |-- pages
32
- | |-- about
33
- | |-- body.html
34
- | |-- meta.yml
35
- |
36
- |-- posts
37
- |-- some-article
38
- |-- index.html
39
- |-- nav.html
27
+ .
28
+ ├── info.yml
29
+ ├── nav.yml
30
+ ├── pages
31
+ │   └── about
32
+ │   ├── body.html
33
+ │   ├── meta.yml
34
+ │   ├── person-1.html
35
+ │   ├── person-2.html
36
+ │   └── person-3.html
37
+ └── posts
38
+ └── random-article
39
+ ├── index.html
40
+ └── nav.html
40
41
 
41
42
 
42
43
  You could then query your data like so:
43
44
 
44
45
  ```ruby
45
- Cabi.read('pages:about:body') # returns contents of page/about/body.html
46
- Cabi.read('pages:about:meta:foo:bar') # returns contents of ['foo']['bar'] in page/about/meta.yml hash
47
- Cabi.read('info:foo:bar:baz') # returns contents of ['foo']['bar']['baz'] in info.yml hash
48
- Cabi.read('posts:some-article:index') # returns contents of posts/some-article/index.html
49
- Cabi.read('posts:some-article:index.html') # returns contents of posts/some-article/index.html
46
+ # Simple selection
47
+ Cabi.read('pages:about:body') # contents of page/about/body.html
48
+
49
+ # Selection with/without explicit file extension
50
+ Cabi.read('posts:some-article:index') # contents of posts/some-article/index.html
51
+ Cabi.read('posts:some-article:index.html') # contents of posts/some-article/index.html
52
+
53
+ # Bulk selection - since Cabi uses dir globbing
54
+ # under the hood, any valid dir glob in your
55
+ # selection will work.
56
+ Cabi.read('pages:about:*') # an array of body.html and meta.yml contents
57
+ Cabi.read('pages:about:person-*') # an array of all person-* html files
58
+ Cabi.read('**/**') # an array of all files in cache
59
+
60
+ # Selection within YAML files
61
+ Cabi.read('pages:about:meta:foo:bar') # contents of ['foo']['bar'] in page/about/meta.yml hash
62
+ Cabi.read('info:foo:bar:baz') # contents of ['foo']['bar']['baz'] in info.yml hash
50
63
  ```
51
64
 
52
65
  ### Custom Cache Directory
@@ -55,13 +68,18 @@ Cabi assumes that your cache directory is either a folder at the top level of yo
55
68
 
56
69
  For instance, if you had a folder called `super-cache` located inside of your project's root that had a file called `.cabi-cache` inside of it, this folder would be treated as your cache directory.
57
70
 
58
- ### Questions?
59
-
60
- Find me online [@brianmgonzalez](http://twitter.com/brianmgonzalez)
61
-
62
71
  ### Icon <img src="https://rawgithub.com/briangonzalez/cabi-gem/master/data/cabi.svg" width=20 style="margin-right: 10px">
63
72
 
64
73
  [Cabi Icon](http://thenounproject.com/noun/file-cabinet/#icon-No22117) designed by Michela Tannoia, from The Noun Project.
65
74
 
66
75
  ### Tests
67
- Run the test suite by running `rake test` in the parent directory.
76
+ Run the test suite by running `rake test` in the parent directory.
77
+
78
+ ### Building Gem Manually
79
+ ```bash
80
+ $ gem build cabi.gemspec
81
+ $ gem install ./cabi-<VERSION>.gem
82
+ ```
83
+
84
+ ### Questions?
85
+ Find me online [@brianmgonzalez](http://twitter.com/brianmgonzalez)
data/bin/cabi CHANGED
@@ -2,7 +2,7 @@
2
2
  require "thor"
3
3
  require "fileutils"
4
4
 
5
- CACHE_DIR = 'cabi-cache'
5
+ require File.join( File.expand_path("../..", __FILE__), "lib", "cabi")
6
6
 
7
7
  class CabiCLI < Thor
8
8
 
@@ -10,23 +10,29 @@ class CabiCLI < Thor
10
10
  method_options :mock => :boolean, :target => :string
11
11
  def init
12
12
 
13
- target = options[:target] || CACHE_DIR
13
+ target = options[:target] || Cabi::CABI_CACHE_DIR
14
14
  FileUtils.mkdir( target )
15
15
 
16
16
  if options[:mock]
17
17
  f = File.expand_path( "../../data/cabi-cache.tar.gz", __FILE__ )
18
- `tar -xf #{f} --strip-components=1 -C #{target}`
18
+ `tar -xf #{f} --strip-components=2 -C #{target}`
19
19
  end
20
20
 
21
+ puts " ** Cabi cache created in #{target}"
21
22
  end
22
23
 
23
24
  desc "clean", "Remove Cabi cache directory"
24
25
  method_options :force => :boolean
25
26
  def clean
26
27
  if options[:force]
27
- FileUtils.rm_rf( CACHE_DIR ) if options[:force]
28
+ begin
29
+ FileUtils.rm_rf( Cabi.cache_dir )
30
+ FileUtils.rm_rf( Cabi::CABI_CACHE_DIR)
31
+ puts " ** Cleaned cabi cache"
32
+ rescue Exception => e
33
+ end
28
34
  else
29
- puts "Not cleaning without --force"
35
+ puts " ** Not cleaning cabi cache without --force"
30
36
  end
31
37
  end
32
38
 
data/cabi.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'cabi'
3
- s.version = '0.1.0'
3
+ s.version = '0.1.1'
4
4
  s.summary = "A simple, flat-file datastore for Ruby."
5
5
 
6
6
  s.description = "Cabi is a flat-file datastore where data is stored by directory stucture and accessed by colon-delimited strings."
Binary file
data/lib/cabi.rb CHANGED
@@ -8,6 +8,7 @@ end
8
8
  module Cabi
9
9
 
10
10
  DELIMITER = ':'
11
+ BULK_SELECTOR = '*'
11
12
  YAML_EXT = '.yml'
12
13
  CABI_CACHE_ID = '.cabi-cache'
13
14
  CABI_CACHE_DIR = './cabi-cache'
data/lib/cache.rb CHANGED
@@ -14,10 +14,8 @@ module Cabi
14
14
  dir = false
15
15
 
16
16
  Dir.foreach('.') do |item|
17
- next if item == '.' or item == '..'
18
- if File.directory?(item) and File.exists?( File.join(item, CABI_CACHE_ID) )
19
- dir = item
20
- end
17
+ next if item == '..' or !File.directory?(item)
18
+ dir = item if File.exists?( File.join(item, CABI_CACHE_ID) )
21
19
  break if dir
22
20
  end
23
21
 
@@ -28,7 +26,8 @@ module Cabi
28
26
 
29
27
  # Helpers for setting/getting cache dir.
30
28
  def self.cache_dir
31
- @@cache_dir ||= Cache.user_cache_dir || CABI_CACHE_DIR
29
+ dir = Cache.user_cache_dir || CABI_CACHE_DIR
30
+ @@cache_dir = File.expand_path(dir)
32
31
  raise LoadError.new "Could not find cabi cache folder!" unless File.exists? @@cache_dir
33
32
  @@cache_dir
34
33
  end
data/lib/datafile.rb CHANGED
@@ -3,9 +3,10 @@ module Cabi
3
3
  class DataFile
4
4
 
5
5
  def self.contents(id)
6
- return File.read( self.file(id) ) if self.file_exists?(id)
7
- return File.read( self.yaml_file(id) ) if self.yaml_exists?(id)
8
- return File.read( self.non_extension_file(id) ) if self.non_extension_file(id)
6
+ return self.bulk_selection(id) if self.bulk_selection?(id)
7
+ return File.read( self.file(id) ) if self.file_exists?(id)
8
+ return YAML.load(File.read( self.yaml_file(id) )) if self.yaml_exists?(id)
9
+ return File.read( self.non_extension_file(id) ) if self.non_extension_file(id)
9
10
  return self.sub_yaml(id)
10
11
  end
11
12
 
@@ -23,18 +24,6 @@ module Cabi
23
24
  File.open( file, 'w') {|f| f.write(content) }
24
25
  end
25
26
 
26
- def self.exists?(id)
27
- self.file_exists?(id) or self.yaml_exists?(id)
28
- end
29
-
30
- def self.file_exists?(id)
31
- File.exists?( self.file(id) )
32
- end
33
-
34
- def self.yaml_exists?(id)
35
- File.exists?( self.yaml_file(id) )
36
- end
37
-
38
27
  def self.file_yaml_or_non_extension_file(id)
39
28
  return self.file(id) if self.file_exists?(id)
40
29
  return self.yaml_file(id) if self.yaml_exists?(id)
@@ -43,19 +32,13 @@ module Cabi
43
32
  end
44
33
 
45
34
  def self.file(id)
46
- id = id.split( DELIMITER )
47
- id = [Cabi.cache_dir] + id
48
- File.join( *id )
35
+ File.join( *self.id_array(id) )
49
36
  end
50
37
 
51
38
  def self.yaml_file(id)
52
39
  File.join( self.file(id) + YAML_EXT )
53
40
  end
54
41
 
55
- def self.file_parent(id)
56
- File.dirname( self.file(id) )
57
- end
58
-
59
42
  def self.non_extension_file(id)
60
43
  base = id.split( DELIMITER ).last
61
44
 
@@ -68,9 +51,46 @@ module Cabi
68
51
  file
69
52
  end
70
53
 
54
+ def self.bulk_selection(id)
55
+ contents = []
56
+
57
+ Dir.glob( File.join( *self.id_array(id) ) ).each do |f|
58
+ next if f == '.' or f == '..' or File.directory?(f)
59
+ contents << File.read(f) if File.exists?(f)
60
+ end
61
+
62
+ contents
63
+ end
64
+
65
+ def self.id_array(id)
66
+ id = id.split( DELIMITER )
67
+ [Cabi.cache_dir] + id
68
+ end
69
+
70
+ def self.exists?(id)
71
+ self.file_exists?(id) or self.yaml_exists?(id)
72
+ end
73
+
74
+ def self.file_exists?(id)
75
+ File.exists?( self.file(id) )
76
+ end
77
+
78
+ def self.yaml_exists?(id)
79
+ File.exists?( self.yaml_file(id) )
80
+ end
81
+
82
+ def self.file_parent(id)
83
+ File.dirname( self.file(id) )
84
+ end
85
+
86
+ def self.bulk_selection?(id)
87
+ self.id_array(id).join('').index /\*/
88
+ end
89
+
71
90
  def self.sub_yaml(id)
72
91
  id = id.split( DELIMITER )
73
92
  val = false
93
+
74
94
  id.each_with_index do |key, index|
75
95
  break if val
76
96
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cabi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Gonzalez