parametron 0.3.0 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ead50f33629a792269e16c9daf73a101bf8abfe5
4
- data.tar.gz: 9e3b6a0e03cb2888e34b14b5fbfd3d885f29d907
3
+ metadata.gz: 7dcdccf1f3be6a66dcc1d9e908438f12e3455727
4
+ data.tar.gz: 18a54b7cbabf0e427e35b889643a9014133d84d4
5
5
  SHA512:
6
- metadata.gz: cedeb7a75197ff92e15528204dd57535b86f0882d457e7fb7059d6ca614b6f2e8594121ae14addd9b1a580005270544c5623f134433c6ca8cae31c2dda96fdb9
7
- data.tar.gz: 35708d414d02d7a497a482979db61aebaa89c05a37708a33550ac192bec40ece64d7194ffcf28ff657b43246acd17990873769aef9a8e1c351feeb26b44e2dd6
6
+ metadata.gz: beba071b20a06987b53f225baa2c93d6699e6494209002810d036d65de832d88082ed9dcb3081bfc22e68a5bfcc795d2f14e04f86180a493864bbc64226ee8dc
7
+ data.tar.gz: a4ab379a513fc033c48f342f254f34b8b67c8b376ba6e2da4628089e3d6ea3e2b0ce915282464fc14043561be27745c9164a703d358f1c7fdb1f01a4c7da865b
@@ -0,0 +1 @@
1
+ parametron
@@ -0,0 +1 @@
1
+ ruby-2.2.2
@@ -1,4 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
- - 2.0.0
4
+ - 2.0.0
5
+ - 2.2.0
6
+ - 2.3.0
@@ -1,3 +1,7 @@
1
+ 0.3.5 / 2015-12-29
2
+ ==================
3
+ * Implement call cast lambda with parameters
4
+
1
5
  0.3.0 / 2014-07-30
2
6
  ==================
3
7
  * Implement casting input parameter
data/README.md CHANGED
@@ -1,45 +1,134 @@
1
1
  # Parametron
2
2
 
3
+ [![Code Climate](https://codeclimate.com/github/svenyurgensson/parametron/badges/gpa.svg)](https://codeclimate.com/github/svenyurgensson/parametron)
4
+ [![Test Coverage](https://codeclimate.com/github/svenyurgensson/parametron/badges/coverage.svg)](https://codeclimate.com/github/svenyurgensson/parametron/coverage)
5
+
3
6
  This simple library implements DSL for validating and type casting input parameters for method.
4
7
 
5
8
  ## Installation
6
9
 
7
10
  Add this line to your application's Gemfile:
8
11
 
9
- gem 'parametron'
12
+ gem 'parametron', '>= 0.3'
10
13
 
11
14
  And then execute:
12
15
 
13
16
  $ bundle
14
17
 
15
- Or install it yourself as:
18
+ Or install it as:
16
19
 
17
20
  $ gem install parametron
18
21
 
19
22
  ## Usage
20
23
 
21
24
  ```ruby
22
-
23
25
  class VictimStrict
24
- include Parametron
26
+ include Parametron # <- should be at the top of class/module
25
27
 
26
28
  params_for(:fetch, strict: true) do
27
29
  required :city, validator: /\w+/
28
30
  required :year, validator: /\d{4}/
29
- optional :title, validator: ->(str){ str != "Moscow" }
31
+ optional :title, validator: ->(str){ str != "Moscow" }, cast: ->(str){ str.upase }
30
32
  optional :number, validator: /\d+/, default: 42
31
33
  optional :gears, default: ->(obj){ choose_by(obj) }
32
34
  optional :weel_d, as: :weel_diameter
35
+ optional :load, cast: Float
33
36
  end
34
37
 
35
38
  def fetch params
36
- # .. do something useful
39
+ # .. do something useful with params
37
40
  end
38
41
  end
39
42
  ```
40
43
 
41
- See `spec/parametron_spec` how this library suppossed to work.
42
- All hackers do this and you should too!
44
+ The main aim of this small gem is to implement base AOP (aspect oriented programming) to separate validation, conversion and setting defaults for methods from real businnes logic which should be the only body of that method without validators, convertors, default setters overhead.
45
+
46
+ In order to get this functionality you only need to include `Parametron` module into top of your class and after that describe desired incoming parameters for method.
47
+
48
+ ```ruby
49
+ class Example
50
+ include Parametron # <- should be at the top of class/module
51
+
52
+ params_for(:fetch) do
53
+ ...
54
+ end
55
+ end
56
+ ```
57
+
58
+ Class method `params_for` accepts one or two arguments: first is symbolized name of method of interest and second one (optional) is hash of two options:
59
+
60
+ * `strict` which is defaultly set to `false`
61
+ * `reject` which is defaultly set to `false` too
62
+
63
+ `strict` when set to `true` means that when you call your method and try to send unknown (not described) key - it raise error `Parametron::ExcessParameter`
64
+ `reject` when set to `true` reject undescribed keys from method arguments
65
+
66
+ ```ruby
67
+ class ExampleStrict
68
+ include Parametron # <- should be at the top of class/module
69
+
70
+ params_for(:fetch, strict: true) do
71
+ optional :one
72
+ end
73
+
74
+ def fetch par
75
+ end
76
+ end
77
+
78
+ ex = ExampleStrict.new.fetch({unexpected: 'argument'})
79
+ # => raises Parametron::ExcessParameter
80
+ ```
81
+
82
+ ```ruby
83
+ class ExampleReject
84
+ include Parametron # <- should be at the top of class/module
85
+
86
+ params_for(:fetch, reject: true) do
87
+ optional :one
88
+ end
89
+
90
+ def fetch par
91
+ par
92
+ end
93
+ end
94
+
95
+ ex = ExampleReject.new.fetch({one: '1', unexpected: 'argument'})
96
+ p ex
97
+ # => {one: '1'}
98
+ ```
99
+
100
+ Method `params_for` should provide description block where you describe required and optional parameters for given method:
101
+
102
+ ```ruby
103
+ params_for(:fetch) do
104
+ required :city, validator: /\w+/
105
+ required :year, validator: /\d{4}/
106
+ optional :title, validator: ->(str){ str != "Moscow" }, cast: ->(str){ str.upase }
107
+ optional :number, validator: /\d+/, default: 42
108
+ optional :gears, default: ->(obj){ choose_by(obj) }
109
+ optional :weel_d, as: :weel_diameter
110
+ optional :load, cast: Float
111
+ end
112
+ ```
113
+
114
+ When parameter is `required` it must present in argument hash.
115
+ Instead, parameter described as `optional` might present but don't must.
116
+
117
+ All of that parameter descriptors can have such refinements:
118
+
119
+ * `validator` with `Regexp` or `proc` predicat to be sure that input parameter is valid
120
+ * `cast` which could be one of: `Integer`, `Float`, `proc` to convert input parameter
121
+ * `default` to set input parameter to its default value in case if it not presents
122
+ * `as` to rename input parameter name to new one
123
+
124
+
125
+ When `cast` options is a lambda/proc you could have current input hash and (if need)
126
+ original input hash. It is depends of arity of lambda you provide.
127
+ If your `cast` lambda arity is 1 then it receive current value to cast; when its arity is 2
128
+ then it receive |current_value, current_casted_parameters| and when its arity is 3
129
+ it will get |current_value, current_casted_parameters, original_params|
130
+ This could be handy when you want to have access to other parameters from input hash already
131
+ casted.
43
132
 
44
133
 
45
134
  ## Contributing
@@ -25,7 +25,9 @@ module Parametron
25
25
  return if name != @_method_name or instance_variable_get(:"@_METHOD_#{name}_WRAPPED")
26
26
  instance_variable_set(:"@_METHOD_#{name}_WRAPPED", true)
27
27
  original = instance_method(name.to_sym)
28
+
28
29
  remove_method(name.to_sym)
30
+
29
31
  define_method(name) do |params={}|
30
32
  new_params = _rename_params!(_cast!(_validate!(_set_defaults!(params))))
31
33
  original.bind(self).call(new_params)
@@ -45,7 +47,13 @@ module Parametron
45
47
  case
46
48
  when v.cast.to_s == "Integer" then Integer(val)
47
49
  when v.cast.to_s == "Float" then Float(val)
48
- when Proc === v.cast then v.cast.call(val)
50
+ when Proc === v.cast
51
+ case v.cast.arity
52
+ when 0 then v.cast.call()
53
+ when 1 then v.cast.call(val)
54
+ when 2 then v.cast.call(val, new_par)
55
+ else v.cast.call(val, new_par, params)
56
+ end
49
57
  else
50
58
  raise MalformedParams.new("Unknown cast type: '#{v.cast.inspect}'")
51
59
  end
@@ -1,3 +1,3 @@
1
1
  module Parametron
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.4"
3
3
  end
@@ -63,6 +63,21 @@ describe Parametron, "Casting" do
63
63
  res[:name].should == "USE PROC"
64
64
  end
65
65
 
66
+ it 'cast input value using proc and have access to already casted params' do
67
+ class VictimDoubleCast
68
+ include Parametron
69
+ params_for(:fetch) do
70
+ required :year, cast: -> x { Integer(x) + 12 }
71
+ required :name, cast: -> n, c { n.upcase + " #{c[:year]}" }
72
+ end
73
+ def fetch(params); params; end
74
+ end
75
+
76
+ res = VictimDoubleCast.new.fetch(year: '1976', name: 'use proc')
77
+ res[:name].should == "USE PROC 1988"
78
+ end
79
+
80
+
66
81
  context "when params cannot be casted to given class" do
67
82
  it 'raises exception when not Integer' do
68
83
  class Victim3
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parametron
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yury Batenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-30 00:00:00.000000000 Z
11
+ date: 2015-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -61,6 +61,8 @@ extra_rdoc_files: []
61
61
  files:
62
62
  - ".gitignore"
63
63
  - ".rspec"
64
+ - ".ruby-gemset"
65
+ - ".ruby-version"
64
66
  - ".travis.yml"
65
67
  - CHANGELOG.md
66
68
  - Gemfile
@@ -94,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
96
  version: '0'
95
97
  requirements: []
96
98
  rubyforge_project:
97
- rubygems_version: 2.2.2
99
+ rubygems_version: 2.5.1
98
100
  signing_key:
99
101
  specification_version: 4
100
102
  summary: DSL for method arguments validation and casting