straight-jacket 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c4f50c09248e6b6dc64b607d7f25632828172c77
4
+ data.tar.gz: f35414d82ce6d8ad4bc746cee306b4ed8a86fc50
5
+ SHA512:
6
+ metadata.gz: 99a1acc952727d0c179029f78045f798082235bbd9be5ca120db04863088f7cdcdb3e85faae7fa7b3fe56b473f910ee25cf5a3ab9915050efbfa0bad30a43e0f
7
+ data.tar.gz: 1ddb7875449c6661848b34e52bf6dd5562ac0f931aeae712c68b8855dd7d468a3fa06a5024ce06bedbcc12a1b5af95afb10c744299bea921b47410da3f8c10ae
@@ -0,0 +1,7 @@
1
+ require 'straight_jacket/composite'
2
+ require 'straight_jacket/format'
3
+ require 'straight_jacket/equals'
4
+ require 'straight_jacket/integer_parameter'
5
+ require 'straight_jacket/inclusion'
6
+ require 'straight_jacket/required_parameter'
7
+ require 'straight_jacket/collection'
@@ -0,0 +1,30 @@
1
+ module StraightJacket
2
+ class Collection
3
+
4
+ def initialize(param, required = false, &block)
5
+ @param = param
6
+ @required = required
7
+ @validator = block
8
+ end
9
+
10
+ def matches?(request)
11
+ if request.parameters.has_key?(param)
12
+ value = request.parameters[param]
13
+ if value.respond_to?(:all?)
14
+ value.all? do |value|
15
+ validator.call(value)
16
+ end
17
+ else
18
+ false
19
+ end
20
+ else
21
+ !required
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :param, :required, :validator
28
+
29
+ end
30
+ end
@@ -0,0 +1,55 @@
1
+ module StraightJacket
2
+ class Composite
3
+
4
+ def initialize(constrainers, options = {})
5
+ method = options[:method] || :all
6
+ @matcher = build_matcher(method, options, constrainers)
7
+ end
8
+
9
+ def matches?(request)
10
+ matcher.call(request)
11
+ end
12
+
13
+ private
14
+
15
+ attr_reader :matcher
16
+
17
+ def build_matcher(method, options, constrainers)
18
+ case method
19
+ when :all
20
+ lambda do |request|
21
+ constrainers.all? do |constrainer|
22
+ constrainer.matches?(request)
23
+ end
24
+ end
25
+ when :any
26
+ lambda do |request|
27
+ constrainers.any? do |constrainer|
28
+ constrainer.matches?(request)
29
+ end
30
+ end
31
+ when :at_least
32
+ lambda do |request|
33
+ number_of_matches(request, constrainers) >= options[:count]
34
+ end
35
+ when :exactly
36
+ lambda do |request|
37
+ number_of_matches(request, constrainers) == options[:count]
38
+ end
39
+ else
40
+ raise 'unknown'
41
+ end
42
+ end
43
+
44
+ def number_of_matches(request, constrainers)
45
+ constrainers.reduce(0) do |acc, constrainer|
46
+ if constrainer.matches?(request)
47
+ acc + 1
48
+ else
49
+ acc
50
+ end
51
+ end
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,21 @@
1
+ module StraightJacket
2
+ class Equals
3
+ def initialize(param, value, required = false)
4
+ @param = param
5
+ @expected_value = value
6
+ @required = required
7
+ end
8
+
9
+ def matches?(request)
10
+ if value = request.parameters[param].presence
11
+ expected_value == value
12
+ else
13
+ !required
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :param, :expected_value, :required
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ module StraightJacket
2
+ class Format
3
+
4
+ def initialize(format)
5
+ @format = format.to_sym
6
+ end
7
+
8
+ def matches?(request)
9
+ request.format.symbol == format
10
+ end
11
+
12
+ private
13
+
14
+ attr_reader :format
15
+
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ require 'active_support/core_ext/object'
2
+
3
+ module StraightJacket
4
+ class Inclusion
5
+
6
+ def initialize(param, valid_values, required = false)
7
+ @param = param
8
+ @valid_values = valid_values
9
+ @required = required
10
+ end
11
+
12
+ def matches?(request)
13
+ if value = request.parameters[param].presence
14
+ valid_values.include?(value)
15
+ else
16
+ !required
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :param, :valid_values, :required
23
+
24
+ end
25
+ end
@@ -0,0 +1,32 @@
1
+ require 'active_support/core_ext/object'
2
+ require 'straight_jacket/validator'
3
+
4
+ module StraightJacket
5
+
6
+ class IntegerParameter
7
+ def initialize(param, required = false)
8
+ @param = param
9
+ case
10
+ when !!required == required # is a boolean
11
+ @required = lambda { |request| required }
12
+ when required.is_a?(Proc)
13
+ @required = required
14
+ else
15
+ @required = lambda { |request| false }
16
+ end
17
+ end
18
+
19
+ def matches?(request)
20
+ if value = request.parameters[@param].presence
21
+ Validator.int?(value)
22
+ else
23
+ !required.call(request)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :required
30
+
31
+ end
32
+ end
@@ -0,0 +1,17 @@
1
+ module StraightJacket
2
+ class RequiredParameter
3
+
4
+ def initialize(param)
5
+ @param = param
6
+ end
7
+
8
+ def matches?(request)
9
+ !!request.parameters[@param].presence
10
+ end
11
+
12
+ private
13
+
14
+ attr_reader :param
15
+
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ module StraightJacket
2
+ module Validator
3
+
4
+ def self.int?(value)
5
+ if match = /\d+/.match(value)
6
+ match[0] == value
7
+ else
8
+ false
9
+ end
10
+ end
11
+
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: straight-jacket
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Myles Megyesi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: actionpack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 4.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 4.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.5.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.5.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 10.1.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 10.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 2.14.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 2.14.1
83
+ description: Route constrainer for Rails
84
+ email:
85
+ - myles.megyesi@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - lib/straight_jacket/equals.rb
91
+ - lib/straight_jacket/integer_parameter.rb
92
+ - lib/straight_jacket/required_parameter.rb
93
+ - lib/straight_jacket/inclusion.rb
94
+ - lib/straight_jacket/format.rb
95
+ - lib/straight_jacket/composite.rb
96
+ - lib/straight_jacket/collection.rb
97
+ - lib/straight_jacket/validator.rb
98
+ - lib/straight_jacket.rb
99
+ homepage:
100
+ licenses: []
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.0.3
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Route constrainer for Rails
122
+ test_files: []