condi 0.0.8 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/.yardopts +7 -0
- data/Gemfile +7 -0
- data/Rakefile +11 -0
- data/condi.gemspec +26 -0
- data/lib/condi.rb +2 -65
- data/lib/condi/condi.rb +65 -0
- data/lib/condi/version.rb +3 -0
- data/lib/version.rb +3 -0
- data/test/test_condi.rb +1 -1
- metadata +115 -13
data/.gitignore
ADDED
data/.yardopts
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
# this file is provided in case you wish to setup a development environment for the gem quickly
|
2
|
+
# using 'bundle install'. It is not a requirement of using the gem itself.
|
3
|
+
|
4
|
+
source 'https://rubygems.org'
|
5
|
+
|
6
|
+
# Specify your gem's dependencies in data_hut.gemspec
|
7
|
+
gemspec
|
data/Rakefile
ADDED
data/condi.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/condi/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Larry Kyrala"]
|
6
|
+
gem.email = ["larry.kyrala@gmail.com"]
|
7
|
+
gem.description = %q{Conditional UI predicates for Rails - a clean and simple approach to separate business logic from your views and models.}
|
8
|
+
gem.summary = %q{Lightweight rules engine for Rails}
|
9
|
+
gem.homepage = "https://github.com/coldnebo/condi"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "condi"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Condi::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'actionpack'
|
19
|
+
|
20
|
+
gem.add_development_dependency 'mocha'
|
21
|
+
gem.add_development_dependency 'pry'
|
22
|
+
gem.add_development_dependency 'yard'
|
23
|
+
gem.add_development_dependency 'redcarpet'
|
24
|
+
gem.add_development_dependency 'simplecov'
|
25
|
+
|
26
|
+
end
|
data/lib/condi.rb
CHANGED
@@ -1,65 +1,2 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
# @example
|
4
|
-
# class StoreController
|
5
|
-
# include Condi
|
6
|
-
# ...
|
7
|
-
# end
|
8
|
-
module Condi
|
9
|
-
|
10
|
-
# define a method on the controller which is callable from the related view and returns a true or false value.
|
11
|
-
# @example define a predicate that determines whether or not to show a "free shipping" option.
|
12
|
-
# predicate(:show_free_shipping?) { user.new_customer? && cart.amount > 100 }
|
13
|
-
# @example define a predicate that takes an element of a collection as an argument.
|
14
|
-
# predicate(:shipping?) { |item| @items.count >= 3 &&
|
15
|
-
# item.status == :shipped &&
|
16
|
-
# DeliveryService.status(item.tracking_number) !~ /arrived/ }
|
17
|
-
# @example define a synonym that returns a css class based on item status
|
18
|
-
# synonym(:css_for_item_status) do |item|
|
19
|
-
# if @items.count >= 3 && item.status == :shipped
|
20
|
-
# if DeliveryService.status(item.tracking_number) !~ /arrived/
|
21
|
-
# "shipping"
|
22
|
-
# else
|
23
|
-
# "shipped"
|
24
|
-
# end
|
25
|
-
# else
|
26
|
-
# "processing"
|
27
|
-
# end
|
28
|
-
# end
|
29
|
-
# @param [Symbol] method_name name of the predicate or synonym method. (e.g. :show_action_button?)
|
30
|
-
# @param [Proc] block {} or do...end block.
|
31
|
-
# @note A *synonym* is an alias for defining methods that return values other than true or false.
|
32
|
-
# @note You are not required to end a predicate with a question mark, however it is conventional in Ruby to do so.
|
33
|
-
# @note Predicates can only be called during the request scope they are defined in, otherwise a RuntimeError is raised. This restriction prevents the associated closures from inadvertently leaking previous request data when the controller classes are cached (i.e. in production).
|
34
|
-
# @note Predicates are semantically lambdas and may contain returns.
|
35
|
-
# @see the full example in the <a href="index.html">README</a>.
|
36
|
-
def predicate(method_name, &block)
|
37
|
-
self.class.instance_eval do
|
38
|
-
# this is the request id at the moment the predicate is defined
|
39
|
-
request_id = eval("request.object_id",block.binding)
|
40
|
-
|
41
|
-
# We need to keep the block impl as a method/lambda so that it supports returns
|
42
|
-
# by using this particular invocation of define_method...
|
43
|
-
method_name_impl = "impl_#{method_name}".to_sym
|
44
|
-
define_method(method_name_impl, &block)
|
45
|
-
|
46
|
-
# Next, this invocation of define_method creates a Proc, which
|
47
|
-
# wraps the impl and allows us to check the request id.
|
48
|
-
define_method(method_name) do |*args|
|
49
|
-
# this is the request id at the moment the predicate is called
|
50
|
-
check_request_id = request.object_id
|
51
|
-
# if they don't match, raise an error!
|
52
|
-
unless check_request_id == request_id
|
53
|
-
raise RuntimeError, "predicate '#{method_name}' cannot be called outside of the request scope it was defined in (#{request_id}). please redefine the predicate in this request scope (#{check_request_id}).", caller(2)
|
54
|
-
end
|
55
|
-
send(method_name_impl, *args)
|
56
|
-
end
|
57
|
-
|
58
|
-
# finally, expose the wrapped predicate to the view.
|
59
|
-
helper_method(method_name)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
# a synonym can be used to define methods that return values besides true and false.
|
64
|
-
alias_method :synonym, :predicate
|
65
|
-
end
|
1
|
+
require "condi/version"
|
2
|
+
require "condi/condi"
|
data/lib/condi/condi.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# Include this module in an ActionController to define predicates or synonyms within an action context that
|
2
|
+
# can be used later in the related action view.
|
3
|
+
# @example
|
4
|
+
# class StoreController
|
5
|
+
# include Condi
|
6
|
+
# ...
|
7
|
+
# end
|
8
|
+
module Condi
|
9
|
+
|
10
|
+
# define a method on the controller which is callable from the related view and returns a true or false value.
|
11
|
+
# @example define a predicate that determines whether or not to show a "free shipping" option.
|
12
|
+
# predicate(:show_free_shipping?) { user.new_customer? && cart.amount > 100 }
|
13
|
+
# @example define a predicate that takes an element of a collection as an argument.
|
14
|
+
# predicate(:shipping?) { |item| @items.count >= 3 &&
|
15
|
+
# item.status == :shipped &&
|
16
|
+
# DeliveryService.status(item.tracking_number) !~ /arrived/ }
|
17
|
+
# @example define a synonym that returns a css class based on item status
|
18
|
+
# synonym(:css_for_item_status) do |item|
|
19
|
+
# if @items.count >= 3 && item.status == :shipped
|
20
|
+
# if DeliveryService.status(item.tracking_number) !~ /arrived/
|
21
|
+
# "shipping"
|
22
|
+
# else
|
23
|
+
# "shipped"
|
24
|
+
# end
|
25
|
+
# else
|
26
|
+
# "processing"
|
27
|
+
# end
|
28
|
+
# end
|
29
|
+
# @param [Symbol] method_name name of the predicate or synonym method. (e.g. :show_action_button?)
|
30
|
+
# @param [Proc] block {} or do...end block.
|
31
|
+
# @note A *synonym* is an alias for defining methods that return values other than true or false.
|
32
|
+
# @note You are not required to end a predicate with a question mark, however it is conventional in Ruby to do so.
|
33
|
+
# @note Predicates can only be called during the request scope they are defined in, otherwise a RuntimeError is raised. This restriction prevents the associated closures from inadvertently leaking previous request data when the controller classes are cached (i.e. in production).
|
34
|
+
# @note Predicates are semantically lambdas and may contain returns.
|
35
|
+
# @see the full example in the <a href="index.html">README</a>.
|
36
|
+
def predicate(method_name, &block)
|
37
|
+
self.class.instance_eval do
|
38
|
+
# this is the request id at the moment the predicate is defined
|
39
|
+
request_id = eval("request.object_id",block.binding)
|
40
|
+
|
41
|
+
# We need to keep the block impl as a method/lambda so that it supports returns
|
42
|
+
# by using this particular invocation of define_method...
|
43
|
+
method_name_impl = "impl_#{method_name}".to_sym
|
44
|
+
define_method(method_name_impl, &block)
|
45
|
+
|
46
|
+
# Next, this invocation of define_method creates a Proc, which
|
47
|
+
# wraps the impl and allows us to check the request id.
|
48
|
+
define_method(method_name) do |*args|
|
49
|
+
# this is the request id at the moment the predicate is called
|
50
|
+
check_request_id = request.object_id
|
51
|
+
# if they don't match, raise an error!
|
52
|
+
unless check_request_id == request_id
|
53
|
+
raise RuntimeError, "predicate '#{method_name}' cannot be called outside of the request scope it was defined in (#{request_id}). please redefine the predicate in this request scope (#{check_request_id}).", caller(2)
|
54
|
+
end
|
55
|
+
send(method_name_impl, *args)
|
56
|
+
end
|
57
|
+
|
58
|
+
# finally, expose the wrapped predicate to the view.
|
59
|
+
helper_method(method_name)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# a synonym can be used to define methods that return values besides true and false.
|
64
|
+
alias_method :synonym, :predicate
|
65
|
+
end
|
data/lib/version.rb
ADDED
data/test/test_condi.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: condi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,20 +9,125 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
dependencies:
|
12
|
+
date: 2013-03-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: actionpack
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: mocha
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: pry
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: yard
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: redcarpet
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: simplecov
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
14
110
|
description: Conditional UI predicates for Rails - a clean and simple approach to
|
15
111
|
separate business logic from your views and models.
|
16
|
-
email:
|
112
|
+
email:
|
113
|
+
- larry.kyrala@gmail.com
|
17
114
|
executables: []
|
18
115
|
extensions: []
|
19
116
|
extra_rdoc_files: []
|
20
117
|
files:
|
21
|
-
-
|
22
|
-
-
|
118
|
+
- .gitignore
|
119
|
+
- .yardopts
|
120
|
+
- Gemfile
|
23
121
|
- MIT-LICENSE
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- condi.gemspec
|
125
|
+
- lib/condi.rb
|
126
|
+
- lib/condi/condi.rb
|
127
|
+
- lib/condi/version.rb
|
128
|
+
- lib/version.rb
|
24
129
|
- test/test_condi.rb
|
25
|
-
homepage:
|
130
|
+
homepage: https://github.com/coldnebo/condi
|
26
131
|
licenses: []
|
27
132
|
post_install_message:
|
28
133
|
rdoc_options: []
|
@@ -34,21 +139,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
34
139
|
- - ! '>='
|
35
140
|
- !ruby/object:Gem::Version
|
36
141
|
version: '0'
|
37
|
-
segments:
|
38
|
-
- 0
|
39
|
-
hash: 737326911507565152
|
40
142
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
143
|
none: false
|
42
144
|
requirements:
|
43
145
|
- - ! '>='
|
44
146
|
- !ruby/object:Gem::Version
|
45
147
|
version: '0'
|
46
|
-
requirements:
|
47
|
-
- rails
|
148
|
+
requirements: []
|
48
149
|
rubyforge_project:
|
49
150
|
rubygems_version: 1.8.24
|
50
151
|
signing_key:
|
51
152
|
specification_version: 3
|
52
|
-
summary:
|
153
|
+
summary: Lightweight rules engine for Rails
|
53
154
|
test_files:
|
54
155
|
- test/test_condi.rb
|
156
|
+
has_rdoc:
|