display_case 0.0.1 → 0.0.2

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.
data/README.md CHANGED
@@ -31,7 +31,7 @@ Your exhibits will look something like this:
31
31
  ``` ruby
32
32
  # app/exhibits/league_exhibit.rb
33
33
 
34
- class LeagueExhibit < Exhibit
34
+ class LeagueExhibit < DisplayCase::Exhibit
35
35
  def self.applicable_to?(object)
36
36
  object.class.name == 'League'
37
37
  end
@@ -46,6 +46,7 @@ Then in your controller, where you're instantiating a League, wrap it in a call
46
46
  ``` ruby
47
47
  # app/controllers/leagues_controller.rb
48
48
  class LeaguesController < ApplicationController
49
+ include DisplayCase::ExhibitsHelper
49
50
  # ...
50
51
  def index
51
52
  # display_case automatically wraps the individual objects contained in
@@ -1,53 +1,55 @@
1
1
  require_relative 'exhibit'
2
2
 
3
- class EnumerableExhibit < Exhibit
4
- include Enumerable
5
-
6
- def self.applicable_to?(object)
7
- # ActiveRecord::Relation, surprisingly, is not Enumerable. But it
8
- # behaves sufficiently similarly for our purposes.
9
- object_is_any_of?(object, 'Enumerable', 'ActiveRecord::Relation')
10
- end
3
+ module DisplayCase
4
+ class EnumerableExhibit < Exhibit
5
+ include Enumerable
6
+
7
+ def self.applicable_to?(object)
8
+ # ActiveRecord::Relation, surprisingly, is not Enumerable. But it
9
+ # behaves sufficiently similarly for our purposes.
10
+ object_is_any_of?(object, 'Enumerable', 'ActiveRecord::Relation')
11
+ end
11
12
 
12
- # Wrap an Enumerable method which returns another collection
13
- def self.exhibit_enum(*method_names, &post_process)
14
- post_process ||= ->(result){exhibit(result)}
15
- method_names.each do |method_name|
16
- define_method(method_name) do |*args, &block|
17
- result = __getobj__.public_send(method_name, *args, &block)
18
- instance_exec(result, &post_process)
13
+ # Wrap an Enumerable method which returns another collection
14
+ def self.exhibit_enum(*method_names, &post_process)
15
+ post_process ||= ->(result){exhibit(result)}
16
+ method_names.each do |method_name|
17
+ define_method(method_name) do |*args, &block|
18
+ result = __getobj__.public_send(method_name, *args, &block)
19
+ instance_exec(result, &post_process)
20
+ end
19
21
  end
20
22
  end
21
- end
22
- private_class_method :exhibit_enum
23
+ private_class_method :exhibit_enum
23
24
 
24
- exhibit_query :[], :fetch, :slice, :values_at, :last
25
- exhibit_enum :select, :grep, :reject, :to_enum, :sort, :sort_by, :reverse
26
- exhibit_enum :partition do |result|
27
- result.map{|group| exhibit(group)}
28
- end
29
- exhibit_enum :group_by do |result|
30
- result.inject({}) { |h,(k,v)|
31
- h.merge!(k => exhibit(v))
32
- }
33
- end
25
+ exhibit_query :[], :fetch, :slice, :values_at, :last
26
+ exhibit_enum :select, :grep, :reject, :to_enum, :sort, :sort_by, :reverse
27
+ exhibit_enum :partition do |result|
28
+ result.map{|group| exhibit(group)}
29
+ end
30
+ exhibit_enum :group_by do |result|
31
+ result.inject({}) { |h,(k,v)|
32
+ h.merge!(k => exhibit(v))
33
+ }
34
+ end
34
35
 
35
- def each(*)
36
- super do |e|
37
- yield exhibit(e)
36
+ def each(*)
37
+ super do |e|
38
+ yield exhibit(e)
39
+ end
38
40
  end
39
- end
40
41
 
41
- # `render '...', :collection => self` will call #to_ary on this
42
- # before rendering, so we need to be prepared.
43
- def to_ary
44
- self
45
- end
42
+ # `render '...', :collection => self` will call #to_ary on this
43
+ # before rendering, so we need to be prepared.
44
+ def to_ary
45
+ self
46
+ end
46
47
 
47
- def render(template)
48
- inject(ActiveSupport::SafeBuffer.new) { |output,element|
49
- output << element.render(template)
50
- }
51
- end
48
+ def render(template)
49
+ inject(ActiveSupport::SafeBuffer.new) { |output,element|
50
+ output << element.render(template)
51
+ }
52
+ end
52
53
 
54
+ end
53
55
  end
@@ -2,129 +2,131 @@ require 'delegate'
2
2
  require 'active_support/core_ext'
3
3
  require 'display_case/railtie' if defined?(Rails)
4
4
 
5
- class Exhibit < SimpleDelegator
6
- @@exhibits = []
5
+ module DisplayCase
6
+ class Exhibit < SimpleDelegator
7
+ @@exhibits = []
7
8
 
8
- def self.exhibits
9
- @@exhibits
10
- end
9
+ def self.exhibits
10
+ @@exhibits
11
+ end
11
12
 
12
- def self.inherited(child)
13
- @@exhibits << child
14
- end
13
+ def self.inherited(child)
14
+ @@exhibits << child
15
+ end
15
16
 
16
- def self.exhibit(object, context)
17
- return object if exhibited_object?(object)
18
- Rails.logger.debug "Registered exhibits: #{@@exhibits}"
19
- Rails.logger.debug "Exhibiting #{object.inspect}"
20
- Rails.logger.debug "Exhibit context: #{context}"
21
- object = Exhibited.new(object, context)
22
- exhibits.inject(object) do |object, exhibit_class|
23
- exhibit_class.exhibit_if_applicable(object, context)
24
- end.tap do |obj|
25
- Rails.logger.debug "Exhibits applied: #{obj.inspect_exhibits}"
17
+ def self.exhibit(object, context)
18
+ return object if exhibited_object?(object)
19
+ Rails.logger.debug "Registered exhibits: #{@@exhibits}"
20
+ Rails.logger.debug "Exhibiting #{object.inspect}"
21
+ Rails.logger.debug "Exhibit context: #{context}"
22
+ object = Exhibited.new(object, context)
23
+ exhibits.inject(object) do |object, exhibit_class|
24
+ exhibit_class.exhibit_if_applicable(object, context)
25
+ end.tap do |obj|
26
+ Rails.logger.debug "Exhibits applied: #{obj.inspect_exhibits}"
27
+ end
26
28
  end
27
- end
28
29
 
29
- def self.exhibit_if_applicable(object, context)
30
- if applicable_to?(object)
31
- new(object, context)
32
- else
33
- object
30
+ def self.exhibit_if_applicable(object, context)
31
+ if applicable_to?(object)
32
+ new(object, context)
33
+ else
34
+ object
35
+ end
34
36
  end
35
- end
36
37
 
37
- def self.applicable_to?(object)
38
- false
39
- end
38
+ def self.applicable_to?(object)
39
+ false
40
+ end
40
41
 
41
- def self.exhibited_object?(object)
42
- object.respond_to?(:exhibited?) && object.exhibited?
43
- end
42
+ def self.exhibited_object?(object)
43
+ object.respond_to?(:exhibited?) && object.exhibited?
44
+ end
44
45
 
45
- def self.exhibit_query(*method_names)
46
- method_names.each do |name|
47
- define_method(name) do |*args, &block|
48
- exhibit(super(*args, &block))
46
+ def self.exhibit_query(*method_names)
47
+ method_names.each do |name|
48
+ define_method(name) do |*args, &block|
49
+ exhibit(super(*args, &block))
50
+ end
49
51
  end
50
52
  end
51
- end
52
- private_class_method :exhibit_query
53
-
54
- # A helper for matching models to classes/modules, intended for use
55
- # in .applicable_to?.
56
- def self.object_is_any_of?(object, *classes)
57
- # What with Rails development mode reloading making class matching
58
- # unreliable, plus wanting to avoid adding dependencies to
59
- # external class definitions if we can avoid it, we just match
60
- # against class/module name strings rather than the actual class
61
- # objects.
62
-
63
- # Note that '&' is the set intersection operator for Arrays.
64
- (classes.map(&:to_s) & object.class.ancestors.map(&:name)).any?
65
- end
66
- private_class_method :object_is_any_of?
53
+ private_class_method :exhibit_query
54
+
55
+ # A helper for matching models to classes/modules, intended for use
56
+ # in .applicable_to?.
57
+ def self.object_is_any_of?(object, *classes)
58
+ # What with Rails development mode reloading making class matching
59
+ # unreliable, plus wanting to avoid adding dependencies to
60
+ # external class definitions if we can avoid it, we just match
61
+ # against class/module name strings rather than the actual class
62
+ # objects.
63
+
64
+ # Note that '&' is the set intersection operator for Arrays.
65
+ (classes.map(&:to_s) & object.class.ancestors.map(&:name)).any?
66
+ end
67
+ private_class_method :object_is_any_of?
67
68
 
68
- attr_reader :context
69
+ attr_reader :context
69
70
 
70
- def initialize(model, context)
71
- @context = context
72
- super(model)
73
- end
71
+ def initialize(model, context)
72
+ @context = context
73
+ super(model)
74
+ end
74
75
 
75
- alias_method :__class__, :class
76
- def class
77
- __getobj__.class
78
- end
76
+ alias_method :__class__, :class
77
+ def class
78
+ __getobj__.class
79
+ end
79
80
 
80
- def exhibit(model)
81
- Exhibit.exhibit(model, context)
82
- end
81
+ def exhibit(model)
82
+ Exhibit.exhibit(model, context)
83
+ end
83
84
 
84
- def to_partial_path
85
- if __getobj__.respond_to?(:to_partial_path)
86
- __getobj__.to_partial_path
87
- else
88
- partialize_name(__getobj__.class.name)
85
+ def to_partial_path
86
+ if __getobj__.respond_to?(:to_partial_path)
87
+ __getobj__.to_partial_path
88
+ else
89
+ partialize_name(__getobj__.class.name)
90
+ end
89
91
  end
90
- end
91
92
 
92
- def render(template)
93
- template.render(:partial => to_partial_path, :object => self)
94
- end
93
+ def render(template)
94
+ template.render(:partial => to_partial_path, :object => self)
95
+ end
95
96
 
96
- def exhibit_chain
97
- inner_exhibits = defined?(super) ? super : []
98
- [__class__] + inner_exhibits
99
- end
97
+ def exhibit_chain
98
+ inner_exhibits = defined?(super) ? super : []
99
+ [__class__] + inner_exhibits
100
+ end
100
101
 
101
- def inspect_exhibits
102
- exhibit_chain.map(&:to_s).join(':')
103
- end
102
+ def inspect_exhibits
103
+ exhibit_chain.map(&:to_s).join(':')
104
+ end
104
105
 
105
- def inspect
106
- "#{inspect_exhibits}(#{__getobj__.inspect})"
107
- end
106
+ def inspect
107
+ "#{inspect_exhibits}(#{__getobj__.inspect})"
108
+ end
108
109
 
109
- def exhibited?
110
- true
111
- end
110
+ def exhibited?
111
+ true
112
+ end
112
113
 
113
- private
114
+ private
114
115
 
115
- # The terminator for the exhibit chain, and a marker that an object
116
- # has been through the exhibit process
117
- class Exhibited < Exhibit
118
- def exhibit_chain
119
- []
120
- end
116
+ # The terminator for the exhibit chain, and a marker that an object
117
+ # has been through the exhibit process
118
+ class Exhibited < Exhibit
119
+ def exhibit_chain
120
+ []
121
+ end
121
122
 
122
- def to_model
123
- __getobj__
123
+ def to_model
124
+ __getobj__
125
+ end
124
126
  end
125
- end
126
127
 
127
- def partialize_name(name)
128
- "/#{name.underscore.pluralize}/#{name.demodulize.underscore}"
128
+ def partialize_name(name)
129
+ "/#{name.underscore.pluralize}/#{name.demodulize.underscore}"
130
+ end
129
131
  end
130
- end
132
+ end
@@ -1,5 +1,7 @@
1
- module ExhibitsHelper
2
- def exhibit(model, context=self)
3
- Exhibit.exhibit(model, context)
1
+ module DisplayCase
2
+ module ExhibitsHelper
3
+ def exhibit(model, context=self)
4
+ Exhibit.exhibit(model, context)
5
+ end
4
6
  end
5
- end
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: display_case
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-23 00:00:00.000000000Z
12
+ date: 2012-05-23 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: An implementation of the Exhibit pattern, as described in Objects on
15
15
  Rails
@@ -47,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
47
  version: '0'
48
48
  requirements: []
49
49
  rubyforge_project:
50
- rubygems_version: 1.8.8
50
+ rubygems_version: 1.8.10
51
51
  signing_key:
52
52
  specification_version: 3
53
53
  summary: An implementation of the Exhibit pattern, as described in Objects on Rails