metastore 0.2.3 → 0.3.0

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: 7cca3216af0666502760ad04279db43a9a8bc57f
4
- data.tar.gz: 768a7cbf250acfd0b2d7f06e87606611d733aa23
3
+ metadata.gz: 17185ce2d6da4fd3375df8d3e7c51eec1875b146
4
+ data.tar.gz: 5dcfcbce2626843877613757859193a108d4efb9
5
5
  SHA512:
6
- metadata.gz: f738de3d865b98b7b7df13bf4d0b9ec70fe561fa72fe845efc8cf67875799a9a311f2af21e27ec6a170410518d2180b8750c1a174329e6df6920b6a658ed1a37
7
- data.tar.gz: 6354a19e99dc4e6aae63122cc77e84feef41ee5c3398c1e44be33fb3fa3e6546e6c21b496672e3f433869e511641fdb2082e52ab9aad610baadf847261859957
6
+ metadata.gz: 04cacb72808ab9a1938a99ac1c5b718afef87b61929d2d80bf327aba5c4b2b095f9fb8f77445eb43a89839f96d9960fafd6e0c79dbd6744a2dc380063407b503
7
+ data.tar.gz: 473ce35a7281f94fb605d13c509a1133aa396eca495ac02b8e060244d7efb05a13fbbb75bb596cfce3e9dee2f06739e666a26081ab652d24ed749c61b9acfa15
data/README.md CHANGED
@@ -1,11 +1,12 @@
1
1
  # Metastore
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/metastore.svg)](http://badge.fury.io/rb/metastore)
4
- [![Code Climate](https://codeclimate.com/github/ashmckenzie/metastore/badges/gpa.svg)](https://codeclimate.com/github/ashmckenzie/metastore)
5
4
  [![Build Status](https://travis-ci.org/ashmckenzie/metastore.svg?branch=wip)](https://travis-ci.org/ashmckenzie/metastore)
5
+ [![Code Climate](https://codeclimate.com/github/ashmckenzie/metastore/badges/gpa.svg)](https://codeclimate.com/github/ashmckenzie/metastore)
6
+ [![Test Coverage](https://codeclimate.com/github/ashmckenzie/metastore/badges/coverage.svg)](https://codeclimate.com/github/ashmckenzie/metastore)
6
7
  [![Dependency Status](https://gemnasium.com/ashmckenzie/metastore.svg)](https://gemnasium.com/ashmckenzie/metastore)
7
8
 
8
- Store and retrieve meta information with ease
9
+ Store and retrieve meta information with ease. Currently YAML and JSON are the supported storage backends.
9
10
 
10
11
  ## Installation
11
12
 
@@ -32,13 +33,30 @@ There are four public methods hanging off `Metastore::Cabinet`:
32
33
  * `#clear!`
33
34
  * `#contents`
34
35
 
36
+ When calling `#get()` or `#set()`:
37
+
38
+ `key` can be expressed as follows:
39
+
40
+ * `key` - basic string key
41
+ * `:key` - basic symbol key (will be converted into string)
42
+ * `key1.key2` - using a `.` here allows nested keys to be defined.
43
+
44
+ When calling `#set()`:
45
+
46
+ `key` can be expressed as described above. The YAML file is also immediately saved.
47
+
35
48
  ### Setup
36
49
 
37
50
  ```ruby
38
51
  require 'metastore'
39
52
 
40
- file = File.join(ENV['HOME'], '.metadata.yaml')
41
- store = Metastore::Cabinet.new(file)
53
+ file = File.join(ENV['HOME'], '.metastore.yaml')
54
+
55
+ # YAML (default)
56
+ store = Metastore::Cabinet.new(file, storage_type: :yaml)
57
+
58
+ # JSON
59
+ store = Metastore::Cabinet.new(file, storage_type: :json)
42
60
  ```
43
61
 
44
62
  ### Basic example
@@ -56,6 +74,9 @@ store.get('key')
56
74
  store.set('key', 'value')
57
75
  => true
58
76
 
77
+ store.get('key')
78
+ => "value"
79
+
59
80
  store.contents
60
81
  => {"key"=>"value"}
61
82
 
@@ -86,6 +107,9 @@ store.contents
86
107
  store.set('key3.key4', { 'key' => 'value' })
87
108
  => true
88
109
 
110
+ store.get('key1.key2')
111
+ => "key.key2.value"
112
+
89
113
  store.contents
90
114
  => {"key1"=>{"key2"=>"key.key2.value"}, "key3"=>{"key4"=>{"key"=>"value"}}}
91
115
  ```
@@ -103,13 +127,19 @@ store['key1.key2']
103
127
  => nil
104
128
 
105
129
  store['key1.key2'] = 'key.key2.value'
106
- => true
130
+ => "key.key2.value"
131
+
132
+ store['key1.key2']
133
+ => "key.key2.value"
107
134
 
108
135
  store.contents
109
136
  => {"key1"=>{"key2"=>"key.key2.value"}}
110
137
 
111
138
  store['key3.key4'] = { 'key' => 'value' }
112
- => true
139
+ => {"key"=>"value"}
140
+
141
+ store['key3.key4']
142
+ => {"key"=>"value"}
113
143
 
114
144
  store.contents
115
145
  => {"key1"=>{"key2"=>"key.key2.value"}, "key3"=>{"key4"=>{"key"=>"value"}}}
data/lib/metastore.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'metastore/version'
2
2
  require 'metastore/errors'
3
3
  require 'metastore/cabinet'
4
+ require 'metastore/storage'
4
5
 
5
6
  module Metastore
6
7
  end
@@ -1,12 +1,12 @@
1
1
  require 'fileutils'
2
2
  require 'pathname'
3
- require 'yaml'
4
3
 
5
4
  module Metastore
6
5
  class Cabinet
7
6
 
8
- def initialize(file)
7
+ def initialize(file, storage_type: :yaml)
9
8
  @file = Pathname.new(file).expand_path
9
+ @storage_type = storage_type
10
10
  end
11
11
 
12
12
  def get(key)
@@ -17,16 +17,14 @@ module Metastore
17
17
  current_contents = contents
18
18
  set_key_and_value(current_contents, split_key(key), value)
19
19
  save!(current_contents)
20
- true
21
20
  end
22
21
 
23
22
  def clear!
24
23
  save!({})
25
- true
26
24
  end
27
25
 
28
26
  def contents
29
- file.exist? ? YAML.load(file.read) || {} : {}
27
+ storage.contents || {}
30
28
  end
31
29
 
32
30
  alias_method :[], :get
@@ -34,7 +32,11 @@ module Metastore
34
32
 
35
33
  private
36
34
 
37
- attr_reader :file
35
+ attr_reader :file, :storage_type
36
+
37
+ def storage
38
+ @store || StorageFactory.from_sym(storage_type).new(file)
39
+ end
38
40
 
39
41
  def split_key(key)
40
42
  key.to_s.split('.')
@@ -45,7 +47,7 @@ module Metastore
45
47
  input[current_key] = {} unless input[current_key]
46
48
 
47
49
  if key.empty?
48
- input[current_key] = recursive_stringify_keys(value)
50
+ input[current_key] = stringify_keys(value)
49
51
  input
50
52
  else
51
53
  input[current_key] = {} unless input[current_key].is_a?(Hash)
@@ -53,16 +55,16 @@ module Metastore
53
55
  end
54
56
  end
55
57
 
56
- def recursive_stringify_keys(input)
58
+ def stringify_keys(input)
57
59
  case input
58
60
  when Hash
59
61
  Hash[
60
62
  input.map do |k, v|
61
- [ k.respond_to?(:to_s) ? k.to_s : k, recursive_stringify_keys(v) ]
63
+ [ k.respond_to?(:to_s) ? k.to_s : k, stringify_keys(v) ]
62
64
  end
63
65
  ]
64
66
  when Enumerable
65
- input.map { |v| recursive_stringify_keys(v) }
67
+ input.map { |v| stringify_keys(v) }
66
68
  else
67
69
  input.is_a?(Symbol) ? input.to_s : input
68
70
  end
@@ -70,7 +72,8 @@ module Metastore
70
72
 
71
73
  def save!(new_values)
72
74
  FileUtils.mkdir_p(file.dirname) unless file.exist?
73
- File.open(file.to_s, 'w') { |f| f.write(new_values.to_yaml) }
75
+ storage.save!(new_values)
76
+ true
74
77
  rescue => e
75
78
  raise Errors::CabinetCannotSet.new(e.message)
76
79
  end
@@ -1,5 +1,6 @@
1
1
  module Metastore
2
2
  module Errors
3
3
  class CabinetCannotSet < StandardError; end
4
+ class UnknownStorageType < StandardError; end
4
5
  end
5
6
  end
@@ -0,0 +1,9 @@
1
+ require 'metastore/storage_factory'
2
+ require 'metastore/storage/base'
3
+ require 'metastore/storage/yaml'
4
+ require 'metastore/storage/json'
5
+
6
+ module Metastore
7
+ module Storage
8
+ end
9
+ end
@@ -0,0 +1,33 @@
1
+ require 'yaml'
2
+
3
+ module Metastore
4
+ module Storage
5
+ class Base
6
+
7
+ def initialize(file)
8
+ @file = file
9
+ end
10
+
11
+ def contents
12
+ file.exist? ? read : {}
13
+ end
14
+
15
+ def save!(values)
16
+ File.open(file.to_s, 'w') { |f| f.write(to_write(values)) }
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :file
22
+
23
+ def read
24
+ raise NotImplementedError
25
+ end
26
+
27
+ def to_write(values)
28
+ raise NotImplementedError
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,18 @@
1
+ require 'json'
2
+
3
+ module Metastore
4
+ module Storage
5
+ class JSON < Base
6
+
7
+ private
8
+
9
+ def read
10
+ ::JSON.parse(file.read)
11
+ end
12
+
13
+ def to_write(values)
14
+ values.to_json
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ require 'yaml'
2
+
3
+ module Metastore
4
+ module Storage
5
+ class YAML < Base
6
+
7
+ private
8
+
9
+ def read
10
+ ::YAML.load(file.read)
11
+ end
12
+
13
+ def to_write(values)
14
+ values.to_yaml
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ module Metastore
2
+ class StorageFactory
3
+
4
+ def self.from_sym(symbol)
5
+ klass = symbol.to_s.upcase
6
+ Storage.const_get(klass)
7
+ rescue NameError => e
8
+ raise Errors::UnknownStorageType.new(e.message)
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module Metastore
2
- VERSION = "0.2.3"
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metastore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ash McKenzie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-04 00:00:00.000000000 Z
11
+ date: 2015-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -98,6 +98,11 @@ files:
98
98
  - lib/metastore.rb
99
99
  - lib/metastore/cabinet.rb
100
100
  - lib/metastore/errors.rb
101
+ - lib/metastore/storage.rb
102
+ - lib/metastore/storage/base.rb
103
+ - lib/metastore/storage/json.rb
104
+ - lib/metastore/storage/yaml.rb
105
+ - lib/metastore/storage_factory.rb
101
106
  - lib/metastore/version.rb
102
107
  - metastore.gemspec
103
108
  homepage: https://github.com/ashmckenzie/metastore