bogy 1.1.1 → 1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +3 -2
- data/lib/bogy.rb +6 -15
- data/lib/bogy/handler.rb +11 -1
- data/lib/bogy/handler_manager.rb +37 -0
- data/lib/bogy/{file_handler.rb → handlers/file_handler.rb} +5 -0
- data/lib/bogy/{hash_handler.rb → handlers/hash_handler.rb} +4 -0
- data/lib/bogy/{string_handler.rb → handlers/string_handler.rb} +4 -0
- data/lib/bogy/version.rb +1 -1
- data/lib/bogy/writeable.rb +5 -0
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8865b52a183e491a6ce5cee2d5556099688256f3
|
4
|
+
data.tar.gz: da2a4445216566c2be6d14fd1837edfe69532917
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d2fb096231055f06a137e22c201d25cb09cb6b46160876dcebd75dd4773123cc35cc750c1c0475f477502bfe9377dc121652e90bd7105724b6a251e802b49fa
|
7
|
+
data.tar.gz: 06904300341943768debf81b9fe9bf4e81cab8e0af7497479ed1ff3e9d2202a5300efbb7c1fbab6eae2699a5f30eff4fb757c4fc607f9e3083e545ff9f32ea73
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -27,9 +27,10 @@ yaml = Bogy.new(hash: hash)
|
|
27
27
|
string = "---\na: 1\nb: 2\n" # Looks {'a' => 1, 'b' => 2} as hash
|
28
28
|
yaml = Bogy.new(string: string)
|
29
29
|
|
30
|
-
yaml
|
30
|
+
yaml = Bogy.new(file: path)
|
31
|
+
yaml['foo'] = 'bar' # => "bar". Automatically writes changes to file, if you parse file
|
31
32
|
yaml['foo'] # => "bar"
|
32
|
-
yaml.delete('foo') # => "bar". Also automatically writes to file, if you parse file
|
33
|
+
yaml.delete('foo') # => "bar". Also automatically writes changes to file, if you parse file
|
33
34
|
yaml.to_h # => {'a'=>1, 'b'=>2}
|
34
35
|
yaml.to_yaml # => ---\na: 1\nb: 2\n"
|
35
36
|
yaml.write_to_file('yaml_file.yml') # Converts to YAML and writes to 'yaml_file.yml' file
|
data/lib/bogy.rb
CHANGED
@@ -1,25 +1,15 @@
|
|
1
1
|
require 'yaml'
|
2
2
|
|
3
|
-
require 'bogy/
|
3
|
+
require 'bogy/handler_manager'
|
4
4
|
require 'bogy/writeable'
|
5
|
-
require 'bogy/file_handler'
|
6
|
-
require 'bogy/hash_handler'
|
7
|
-
require 'bogy/string_handler'
|
8
5
|
|
9
6
|
class Bogy
|
7
|
+
|
10
8
|
def initialize(options = {})
|
11
9
|
# If user provides incorrect number of arguments (not one)
|
12
|
-
|
13
|
-
|
14
|
-
@handler =
|
15
|
-
case
|
16
|
-
when options[:file]
|
17
|
-
FileHandler.new(options[:file])
|
18
|
-
when options[:hash]
|
19
|
-
HashHandler.new(options[:hash])
|
20
|
-
when options[:string]
|
21
|
-
StringHandler.new(options[:string])
|
22
|
-
end
|
10
|
+
raise ArgumentError, "wrong number of arguments (#{options.length} for 1)" unless options.length == 1
|
11
|
+
|
12
|
+
@handler = HandlerManager.find_handler(options)
|
23
13
|
end
|
24
14
|
|
25
15
|
def [](key)
|
@@ -48,4 +38,5 @@ class Bogy
|
|
48
38
|
def write_to_file(path)
|
49
39
|
IO.write(path, @handler.hash.to_yaml)
|
50
40
|
end
|
41
|
+
|
51
42
|
end
|
data/lib/bogy/handler.rb
CHANGED
@@ -1,14 +1,24 @@
|
|
1
1
|
class Bogy
|
2
|
-
|
2
|
+
|
3
|
+
##
|
4
|
+
# Abstract class for handlers (FileHandler, HashHandler...)
|
3
5
|
# Handler is object, which can work with certain data type (string, YAML file and etc.)
|
4
6
|
# and convert it to Ruby hash
|
7
|
+
|
5
8
|
class Handler
|
9
|
+
|
6
10
|
def hash
|
7
11
|
@hash ||= load
|
12
|
+
@hash = {} if @hash.nil?
|
13
|
+
|
14
|
+
@hash
|
8
15
|
end
|
9
16
|
|
17
|
+
##
|
10
18
|
# Abstract method
|
11
19
|
# Used to convert raw YAML data from specified format to Ruby hash
|
20
|
+
|
12
21
|
def load; end
|
22
|
+
|
13
23
|
end
|
14
24
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'bogy/handler'
|
2
|
+
|
3
|
+
class Bogy
|
4
|
+
|
5
|
+
##
|
6
|
+
# The handler manager contains information about all bogy handlers
|
7
|
+
# It also find and return them
|
8
|
+
|
9
|
+
module HandlerManager
|
10
|
+
|
11
|
+
HANDLERS = [
|
12
|
+
:file,
|
13
|
+
:hash,
|
14
|
+
:string
|
15
|
+
]
|
16
|
+
|
17
|
+
def self.find_handler(options)
|
18
|
+
options = options.shift
|
19
|
+
handler_name, arg = options
|
20
|
+
|
21
|
+
if HANDLERS.include? handler_name
|
22
|
+
handler_instance(handler_name, arg)
|
23
|
+
else
|
24
|
+
raise ArgumentError, "invalid argument '#{handler_name}'"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.handler_instance(handler_name, arg)
|
29
|
+
handler_name = handler_name.to_s
|
30
|
+
const_name = handler_name.capitalize << 'Handler'
|
31
|
+
|
32
|
+
require "bogy/handlers/#{handler_name}_handler"
|
33
|
+
Bogy.const_get(const_name).new(arg)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
@@ -1,8 +1,12 @@
|
|
1
1
|
class Bogy
|
2
2
|
class FileHandler < Handler
|
3
|
+
|
3
4
|
include Writeable
|
4
5
|
|
5
6
|
def initialize(path)
|
7
|
+
raise ArgumentError, "value should be of String type, not #{path.class}" unless path.is_a? String
|
8
|
+
raise IOError, "No such file - #{path}" unless File.exist?(path)
|
9
|
+
|
6
10
|
@path = path
|
7
11
|
end
|
8
12
|
|
@@ -13,5 +17,6 @@ class Bogy
|
|
13
17
|
def write
|
14
18
|
IO.write(@path, hash.to_yaml)
|
15
19
|
end
|
20
|
+
|
16
21
|
end
|
17
22
|
end
|
@@ -1,11 +1,15 @@
|
|
1
1
|
class Bogy
|
2
2
|
class StringHandler < Handler
|
3
|
+
|
3
4
|
def initialize(string)
|
5
|
+
raise ArgumentError, "value should be of String type, not #{string.class}" unless string.is_a? String
|
6
|
+
|
4
7
|
@string = string
|
5
8
|
end
|
6
9
|
|
7
10
|
def load
|
8
11
|
YAML.load(@string)
|
9
12
|
end
|
13
|
+
|
10
14
|
end
|
11
15
|
end
|
data/lib/bogy/version.rb
CHANGED
data/lib/bogy/writeable.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bogy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: '1.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Albert Nigmatzianov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -69,10 +69,11 @@ files:
|
|
69
69
|
- Rakefile
|
70
70
|
- bogy.gemspec
|
71
71
|
- lib/bogy.rb
|
72
|
-
- lib/bogy/file_handler.rb
|
73
72
|
- lib/bogy/handler.rb
|
74
|
-
- lib/bogy/
|
75
|
-
- lib/bogy/
|
73
|
+
- lib/bogy/handler_manager.rb
|
74
|
+
- lib/bogy/handlers/file_handler.rb
|
75
|
+
- lib/bogy/handlers/hash_handler.rb
|
76
|
+
- lib/bogy/handlers/string_handler.rb
|
76
77
|
- lib/bogy/version.rb
|
77
78
|
- lib/bogy/writeable.rb
|
78
79
|
homepage: https://github.com/bogem/bogy
|
@@ -95,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
96
|
version: '0'
|
96
97
|
requirements: []
|
97
98
|
rubyforge_project:
|
98
|
-
rubygems_version: 2.4.
|
99
|
+
rubygems_version: 2.4.8
|
99
100
|
signing_key:
|
100
101
|
specification_version: 4
|
101
102
|
summary: Ruby wrapper for YAML
|