strict_options 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +107 -0
  3. data/lib/strict_options.rb +6 -3
  4. metadata +16 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd48360a5944716f242f2c5cc9f260262cad9c16
4
- data.tar.gz: 8244ea83be1f05bf434c302ff9b569302f691bbc
3
+ metadata.gz: b5a74909f2f47d78ccd2ba1de2064a9165415731
4
+ data.tar.gz: 138c32ad3c2c3a355f87c39e1725016f2f800e11
5
5
  SHA512:
6
- metadata.gz: d59a551d0682b058f236e00074300d70f9246c078cbd561fbfb55dba18c11159be4ec51f183ac443d3f8776d72da9fa9f23bf9ca1116594d45db6d4b75619a1a
7
- data.tar.gz: 6c998326ab88f07711277e10e639f976073cc8e20be3bb02bceb852c8f88caa4f94e3fe057af161547d9303007639298907ebbeb74ca7ebdebc451effb77780b
6
+ metadata.gz: 8dc1b2b41b57543edabee7c09cdcea7515669c50f168d1328f07150fcb19912b924ffec36ecc1c4054149574707ffaa6c5f1ca47da0e2265f5cee6db67e944af
7
+ data.tar.gz: 9854ad600084821698a2ddba6998ad77a521e9ba4d24c7f773a885773a668ec7e810ae11cc2f236bf8c6bf26494a929cb4fa4beadb20a7b2ee97c20bf1a16bb8
data/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # StrictOptions
2
+
3
+ ## Description
4
+
5
+ Allow you define strict attributes for options hash.
6
+
7
+ Common practice in Ruby OOP are something like this:
8
+
9
+ ```ruby
10
+ class MyBestClass
11
+ def initialize(name, data, options = {})
12
+ @name = name
13
+ @data = data
14
+ @options = options
15
+ end
16
+
17
+ def some_method
18
+ raise ArgumentError, "No user set" unless options[:user]
19
+ raise ArgumentError, "No blah set" unless options[:blah]
20
+
21
+ # ... do something if user & blah was set
22
+ end
23
+ end
24
+ ```
25
+
26
+ If you want some short method that allow raise `ArgumentError` (or some other Error) for all strict options you can use `strict_options!` method.
27
+
28
+ ## Installation & Setup
29
+
30
+ Gemfile:
31
+ ```ruby
32
+ gem 'strict_options'
33
+ ```
34
+ and run `bundle` in console
35
+
36
+ or for plain ruby-program:
37
+
38
+ ```ruby
39
+ gem install strict_options
40
+ ```
41
+ and in rubyfile:
42
+
43
+ ```ruby
44
+ require 'strict_options'
45
+ ```
46
+
47
+ ## Example
48
+
49
+ ```ruby
50
+ require 'strict_options'
51
+
52
+ class Product
53
+ include StrictOptions
54
+
55
+ def initialize(name, options = {})
56
+ @name = name
57
+ @options = options
58
+ @sku = options.fetch(:sku, nil)
59
+ @price = options.fetch(:price, nil)
60
+ @brand = options.fetch(:brand, nil)
61
+ end
62
+
63
+ def full_name
64
+ strict_options!(:brand, :sku)
65
+ "#{@brand} #{@name} (#{@sku})"
66
+ end
67
+
68
+ def dicounted_price
69
+ strict_options!(:price)
70
+ @price * 0.8
71
+ end
72
+ end
73
+
74
+ ```
75
+ And now you will control options attributes:
76
+
77
+ ```ruby
78
+ p = Product.new("iPhone")
79
+ p.full_name #=> options :brand, :sku are missing (ArgumentError)
80
+ p.discounted_price #=> option :price is missing (ArgumentError)
81
+ ```
82
+ and don't miss any attributes
83
+ ```ruby
84
+ p = Product.new("iPhone", brand: "Apple", sku: "iphone_6s_16gb", price: 649)
85
+ puts p.full_name #=> Apple iPhone (iphone_6s_16gb)
86
+ puts p.discounted_price #=> 519.2
87
+ ```
88
+
89
+ >Hold your hand at API pulse*
90
+
91
+ ## Features
92
+
93
+ You could define you custom `ExceptionClass` and exception message:
94
+ ```ruby
95
+
96
+ class PriceError < StandardError; end
97
+
98
+ def calculate_sale_price
99
+ strict_options!(:price, exception_class: PriceError,
100
+ exception_message: "No price!")
101
+ end
102
+
103
+ calculate_sale_price
104
+ #=> #<PriceError: No price!>
105
+
106
+ ## TODO
107
+ * Some improvements ? =)
@@ -1,9 +1,12 @@
1
+ require 'pry-rails'
2
+
1
3
  module StrictOptions
2
- def strict_options!(*opts)
4
+ def strict_options!(*opts, exception_class: ArgumentError,
5
+ exception_message: nil)
3
6
  @missings = []
4
7
  opts.each { |opt| @missings << ":#{opt}" unless @options[opt] }
5
-
6
- raise ArgumentError, "option#{s} #{@missings.join(', ')} #{is_or_are} missing" if @missings.size > 0
8
+ msg = exception_message || "option#{s} #{@missings.join(', ')} #{is_or_are} missing"
9
+ raise exception_class, msg if @missings.size > 0
7
10
  end
8
11
 
9
12
  private
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strict_options
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Volodya Sveredyuk
@@ -30,12 +30,27 @@ dependencies:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 3.4.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: pry-rails
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
33
47
  description: Define strict attributes for options hash
34
48
  email: sveredyuk@gmail.com
35
49
  executables: []
36
50
  extensions: []
37
51
  extra_rdoc_files: []
38
52
  files:
53
+ - README.md
39
54
  - lib/strict_options.rb
40
55
  homepage: https://github.com/sveredyuk/strict_options
41
56
  licenses: