condi 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +9 -5
  2. data/lib/condi.rb +7 -4
  3. metadata +3 -2
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  Condi
3
3
  =====
4
4
 
5
- Condi is a gem that you use with Rails to make it easier to cleanly implement conditional elements in a view.
5
+ Condi is a gem that you use with Rails to make it easier to cleanly implement conditional elements in a view and offers a clean and simple approach to separate business logic from your views and models.
6
6
 
7
7
  Condi allows you to define boolean-valued *predicates* in the controller that are callable in the view without relying on unneeded instance variables or business logic in the views. Because the predicates are defined dynamically during a controller action, they are easy to find and easy to use without hopping around multiple files.
8
8
 
@@ -48,7 +48,7 @@ For example, say you have a User who has various roles and a shopping Cart that
48
48
  Predicates with Arguments
49
49
  -------------------------
50
50
 
51
- Say you would like to monitor your cart status and highlight items that are shipped but haven't arrived yet. You would like to hand the collection of items off to a partial, but how can you use
51
+ Say you would like to monitor your cart status and highlight items that are shipped but haven't arrived yet if there are three or more items in the cart. You would like to hand the collection of items off to a partial, but how can you use
52
52
  a predicate in this situation? You need a predicate with an argument!
53
53
 
54
54
  `app/controllers/store_controller.rb:`
@@ -59,7 +59,9 @@ a predicate in this situation? You need a predicate with an argument!
59
59
  def items
60
60
  cart = Cart.find(cart_id)
61
61
  @items = cart.items
62
- predicate(:shipping?) { |item| item.status == :shipped && DeliveryService.status(item.tracking_number) !~ /arrived/ }
62
+ predicate(:shipping?) { |item| @items.count >= 3 &&
63
+ item.status == :shipped &&
64
+ DeliveryService.status(item.tracking_number) !~ /arrived/ }
63
65
  end
64
66
  end
65
67
 
@@ -95,7 +97,7 @@ returns the css class we need for a given item?
95
97
  cart = Cart.find(cart_id)
96
98
  @items = cart.items
97
99
  synonym(:css_for_item_status) do |item|
98
- if item.status == :shipped
100
+ if @items.count >= 3 && item.status == :shipped
99
101
  if DeliveryService.status(item.tracking_number) !~ /arrived/
100
102
  "shipping"
101
103
  else
@@ -201,7 +203,7 @@ Advantages
201
203
 
202
204
  * Placing predicates in the Controller allows them to orchestrate multiple Models without breaking encapsulation between Models. The Controller is arguably a better place to define such predicates from an MVC perspective.
203
205
 
204
- * Condi makes it simple to define predicates and synonyms in the Controller and call them from the view without cluttering the helper namespace and creating a maze of unique names for every action. Condi is more flexible.
206
+ * Condi makes it simple to define predicates and synonyms in the Controller and call them from the view without cluttering the helper namespace and creating a maze of unique helper names that are coupled to some action contexts and not others. Condi is more flexible.
205
207
 
206
208
  * Another advantage of the predicate being defined dynamically on the Controller is that the predicate can never be inadvertently called as an action itself. Condi offers better encapsulation of state.
207
209
 
@@ -215,6 +217,8 @@ Disadvantages
215
217
 
216
218
  * It may be awkward to share predicates across multiple actions. But if you think about it, it is awkward to share context as well. Filters are a common solution for both problems. You can define your predicates in a before_filter method to ensure that both the context and the predicates will be sharable.
217
219
 
220
+ * If you aren't using any context from the action (i.e. you pass all your parameters as arguments and don't take advantage of the closure) then you don't really need Condi... it may be simpler/cleaner to just use helpers.
221
+
218
222
 
219
223
  Background
220
224
  ----------
@@ -7,14 +7,16 @@
7
7
  # end
8
8
  module Condi
9
9
 
10
- # define a method on the controller which is callable from the related view.
10
+ # define a method on the controller which is callable from the related view and returns a true or false value.
11
11
  # @example define a predicate that determines whether or not to show a "free shipping" option.
12
12
  # predicate(:show_free_shipping?) { user.new_customer? && cart.amount > 100 }
13
13
  # @example define a predicate that takes an element of a collection as an argument.
14
- # predicate(:shipping?) { |item| item.status == :shipped && DeliveryService.status(item.tracking_number) !~ /arrived/ }
14
+ # predicate(:shipping?) { |item| @items.count >= 3 &&
15
+ # item.status == :shipped &&
16
+ # DeliveryService.status(item.tracking_number) !~ /arrived/ }
15
17
  # @example define a synonym that returns a css class based on item status
16
18
  # synonym(:css_for_item_status) do |item|
17
- # if item.status == :shipped
19
+ # if @items.count >= 3 && item.status == :shipped
18
20
  # if DeliveryService.status(item.tracking_number) !~ /arrived/
19
21
  # "shipping"
20
22
  # else
@@ -26,6 +28,7 @@ module Condi
26
28
  # end
27
29
  # @param [Symbol] method_name name of the predicate or synonym method. (e.g. :show_action_button?)
28
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.
29
32
  # @note You are not required to end a predicate with a question mark, however it is conventional in Ruby to do so.
30
33
  # @see the full example in the <a href="index.html">README</a>.
31
34
  def predicate(method_name, &block)
@@ -35,6 +38,6 @@ module Condi
35
38
  end
36
39
  end
37
40
 
38
- # a synonym is similar to a predicate but returns values besides true and false.
41
+ # a synonym can be used to define methods that return values besides true and false.
39
42
  alias_method :synonym, :predicate
40
43
  end
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.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,8 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2011-12-14 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Conditional UI predicates for Rails
14
+ description: Conditional UI predicates for Rails - a clean and simple approach to
15
+ separate business logic from your views and models.
15
16
  email: larry.kyrala@gmail.com
16
17
  executables: []
17
18
  extensions: []