Class: DisplayCase::Exhibit

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/display_case/exhibit.rb

Direct Known Subclasses

BasicExhibit, EnumerableExhibit, Exhibited

Defined Under Namespace

Classes: Exhibited

Constant Summary collapse

@@exhibits =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, context) ⇒ Exhibit

Returns a new instance of Exhibit.



113
114
115
116
# File 'lib/display_case/exhibit.rb', line 113

def initialize(model, context)
  @context = context
  super(model)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



111
112
113
# File 'lib/display_case/exhibit.rb', line 111

def context
  @context
end

Class Method Details

.applicable_to?(object, context = nil) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/display_case/exhibit.rb', line 73

def self.applicable_to?(object, context=nil)
  false
end

.class_comparatorObject



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/display_case/exhibit.rb', line 90

def self.class_comparator
  @@class_comparator ||= begin
    comparator = nil

    if defined? ::Rails
      config = ::Rails.respond_to?(:config) ? ::Rails.config : ::Rails.application.config
      comparator = NameClassComparator.new unless config.cache_classes
    end

    comparator || IsAClassComparator.new
  end
end

.exhibit(object, context = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/display_case/exhibit.rb', line 24

def self.exhibit(object, context=nil)
  return object if exhibited_object?(object)
  if DisplayCase.configuration.logging_enabled?
    ::Rails.logger.debug "Registered exhibits: #{@@exhibits}"
    ::Rails.logger.debug "Exhibiting #{object.inspect}"
    ::Rails.logger.debug "Exhibit context: #{context}"
  end

  object = BasicExhibit.new(Exhibited.new(object, context), context)

  # done w/ unsimilar first since the last applied exhibit is the top-most one
  sorted_exhibits(context).inject(object) do |object, exhibit_class|
    exhibit_class.exhibit_if_applicable(object, context)
  end.tap do |obj|
    ::Rails.logger.debug "Exhibits applied: #{obj.inspect_exhibits}" if DisplayCase.configuration.logging_enabled?
  end
end

.exhibit_if_applicable(object, context) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/display_case/exhibit.rb', line 65

def self.exhibit_if_applicable(object, context)
  if applicable_to?(object, context)
    new(object, context)
  else
    object
  end
end

.exhibited_object?(object) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/display_case/exhibit.rb', line 77

def self.exhibited_object?(object)
  object.respond_to?(:exhibited?) && object.exhibited?
end

.exhibitsObject



12
13
14
15
16
17
18
# File 'lib/display_case/exhibit.rb', line 12

def self.exhibits
  if DisplayCase.configuration.explicit?
    DisplayCase.configuration.exhibits
  else
    @@exhibits
  end
end

.inherited(child) ⇒ Object



20
21
22
# File 'lib/display_case/exhibit.rb', line 20

def self.inherited(child)
  @@exhibits << child
end

Instance Method Details

#__class__Object



118
# File 'lib/display_case/exhibit.rb', line 118

alias_method :__class__, :class

#__instance_of__?Object



131
# File 'lib/display_case/exhibit.rb', line 131

alias_method :__instance_of__?, :instance_of?

#__kind_of__?Object



123
# File 'lib/display_case/exhibit.rb', line 123

alias_method :__kind_of__?, :kind_of?

#cache(key, options = {}, &block) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/display_case/exhibit.rb', line 161

def cache(key, options = {}, &block)
  if DisplayCase.configuration.cache_store
    DisplayCase.configuration.cache_store.fetch(key, options, &block)
  else
    yield
  end
end

#classObject



119
120
121
# File 'lib/display_case/exhibit.rb', line 119

def class
  __getobj__.class
end

#exhibit(model) ⇒ Object



136
137
138
# File 'lib/display_case/exhibit.rb', line 136

def exhibit(model)
  Exhibit.exhibit(model, context)
end

#exhibit_chainObject



140
141
142
143
# File 'lib/display_case/exhibit.rb', line 140

def exhibit_chain
  inner_exhibits = __getobj__.respond_to?(:exhibit_chain) ? __getobj__.exhibit_chain : []
  [__class__] + inner_exhibits
end

#exhibited?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/display_case/exhibit.rb', line 157

def exhibited?
  true
end

#inspectObject



149
150
151
# File 'lib/display_case/exhibit.rb', line 149

def inspect
  "#{inspect_exhibits}(#{__getobj__.inspect})"
end

#inspect_exhibitsObject



145
146
147
# File 'lib/display_case/exhibit.rb', line 145

def inspect_exhibits
  exhibit_chain.map(&:to_s).join(':')
end

#instance_of?(klass) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/display_case/exhibit.rb', line 132

def instance_of?(klass)
  __getobj__.instance_of?(klass)
end

#kind_of?(klass) ⇒ Boolean Also known as: is_a?

Returns:

  • (Boolean)


124
125
126
# File 'lib/display_case/exhibit.rb', line 124

def kind_of?(klass)
  __getobj__.kind_of?(klass)
end

#render(template, options = {}) ⇒ Object



153
154
155
# File 'lib/display_case/exhibit.rb', line 153

def render(template, options = {})
  template.render(options.reverse_merge(:partial => to_partial_path, :object => exhibit(to_model)))
end