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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, context) ⇒ Exhibit

Returns a new instance of Exhibit.



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

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

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

Class Method Details

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

Returns:

  • (Boolean)


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

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

.class_comparatorObject



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/display_case/exhibit.rb', line 95

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/display_case/exhibit.rb', line 29

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



70
71
72
73
74
75
76
# File 'lib/display_case/exhibit.rb', line 70

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)


82
83
84
# File 'lib/display_case/exhibit.rb', line 82

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

.exhibitsObject



17
18
19
# File 'lib/display_case/exhibit.rb', line 17

def self.exhibits
  @@exhibits
end

.inherited(child) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/display_case/exhibit.rb', line 21

def self.inherited(child)
  if idx = @@exhibits.map(&:name).index(child.name)
    @@exhibits[idx] = child
  elsif !DisplayCase.configuration.explicit?
    @@exhibits << child
  end
end

.initialize_exhibitsObject



11
12
13
14
# File 'lib/display_case/exhibit.rb', line 11

def self.initialize_exhibits
  @@exhibits = []
  @@exhibits = DisplayCase.configuration.exhibits.dup if DisplayCase.configuration.explicit?
end

Instance Method Details

#__class__Object



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

alias_method :__class__, :class

#__instance_of__?Object



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

alias_method :__instance_of__?, :instance_of?

#__kind_of__?Object



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

alias_method :__kind_of__?, :kind_of?

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



166
167
168
169
170
171
172
# File 'lib/display_case/exhibit.rb', line 166

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

#classObject



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

def class
  __getobj__.class
end

#exhibit(model) ⇒ Object



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

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

#exhibit_chainObject



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

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

#exhibited?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/display_case/exhibit.rb', line 162

def exhibited?
  true
end

#inspectObject



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

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

#inspect_exhibitsObject



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

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

#instance_of?(klass) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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

Returns:

  • (Boolean)


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

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

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



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

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