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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 017b3b815d63846fd691bb52d4d00e1cd5a9fbfc
4
- data.tar.gz: 5c179c98447f8da644bf1b18bee7335045b9e262
3
+ metadata.gz: 8865b52a183e491a6ce5cee2d5556099688256f3
4
+ data.tar.gz: da2a4445216566c2be6d14fd1837edfe69532917
5
5
  SHA512:
6
- metadata.gz: 2821f00a1ca2b99d8d9e68aef66fafd60b7761b827560cd81d0038c2c5bd5466b91a3c54cb12065e45c5ef8ac6e85b421b8333168369e836c7e47ad1a2fad546
7
- data.tar.gz: 9dec09f747276eec7aa02e47b75f7f9256893b5c68cb3caa7fdcad774b74f153bffc9810bdbf42953f16eb4a654c5c8d294458b359190cc31a10838d60ae7f19
6
+ metadata.gz: 3d2fb096231055f06a137e22c201d25cb09cb6b46160876dcebd75dd4773123cc35cc750c1c0475f477502bfe9377dc121652e90bd7105724b6a251e802b49fa
7
+ data.tar.gz: 06904300341943768debf81b9fe9bf4e81cab8e0af7497479ed1ff3e9d2202a5300efbb7c1fbab6eae2699a5f30eff4fb757c4fc607f9e3083e545ff9f32ea73
@@ -1,5 +1,9 @@
1
1
  # nehm change log
2
2
 
3
+ ## 1.2
4
+ * Performance tweaks
5
+ * Handle more errors
6
+
3
7
  ## 1.1.1
4
8
  * Handle more errors
5
9
  * Some optimizations
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['foo'] = 'bar' # => "bar". Automatically writes to file, if you parse file
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
@@ -1,25 +1,15 @@
1
1
  require 'yaml'
2
2
 
3
- require 'bogy/handler'
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
- fail ArgumentError, "wrong number of arguments (#{options.length} for 1)" if options.length > 1 || options.empty?
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
@@ -1,14 +1,24 @@
1
1
  class Bogy
2
- # Abstract class for handlers (FileHandler, StringHandler...)
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 HashHandler < Handler
3
+
3
4
  def initialize(hash)
5
+ raise ArgumentError, "value should be of Hash type, not #{hash.class}" unless hash.is_a? Hash
6
+
4
7
  @hash = hash
5
8
  end
6
9
 
7
10
  def load
8
11
  @hash
9
12
  end
13
+
10
14
  end
11
15
  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
@@ -1,3 +1,3 @@
1
1
  class Bogy
2
- VERSION = '1.1.1'.freeze
2
+ VERSION = '1.2'.freeze
3
3
  end
@@ -1,7 +1,12 @@
1
1
  class Bogy
2
+
3
+ ##
2
4
  # Abstract module
3
5
  # Used to specify what handler can write
6
+
4
7
  module Writeable
8
+
5
9
  def write; end
10
+
6
11
  end
7
12
  end
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.1.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-09-13 00:00:00.000000000 Z
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/hash_handler.rb
75
- - lib/bogy/string_handler.rb
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.5.1
99
+ rubygems_version: 2.4.8
99
100
  signing_key:
100
101
  specification_version: 4
101
102
  summary: Ruby wrapper for YAML