class Mystique::Presenter
Constants
- FORMATS
Public Class Methods
for(object, context=nil) { |presenter| ... }
click to toggle source
# File lib/mystique/presenter.rb, line 12 def self.for(object, context=nil) new(object, context).tap { |presenter| yield presenter if block_given? } end
new(object, context)
click to toggle source
# File lib/mystique/presenter.rb, line 7 def initialize(object, context) @__object__ = object @__context__ = context || self.class.context || NullContext end
Private Class Methods
__class_formats__()
click to toggle source
# File lib/mystique/presenter.rb, line 160 def self.__class_formats__ @__class_formats__ ||= __formats__.select {|key, _| key.is_a?(Class)} end
__formats__()
click to toggle source
# File lib/mystique/presenter.rb, line 144 def self.__formats__ FORMATS end
__formatted_methods__()
click to toggle source
# File lib/mystique/presenter.rb, line 118 def self.__formatted_methods__ @__formatted_methods__ ||= [] end
__presented_collections__()
click to toggle source
# File lib/mystique/presenter.rb, line 114 def self.__presented_collections__ @__presented_collections__ ||= [] end
__presented_methods__()
click to toggle source
# File lib/mystique/presenter.rb, line 110 def self.__presented_methods__ @__presented_methods__ ||= [] end
__regex_formats__()
click to toggle source
# File lib/mystique/presenter.rb, line 152 def self.__regex_formats__ @__regex_formats__ ||= __formats__.select {|key, _| key.is_a?(Regexp)} end
apply_format(matcher, value=nil, &block)
click to toggle source
# File lib/mystique/presenter.rb, line 83 def self.apply_format(matcher, value=nil, &block) __formats__[matcher] = block_given? ? block : value end
context(ctx=Undefined)
click to toggle source
# File lib/mystique/presenter.rb, line 78 def self.context(ctx=Undefined) @__context__ = ctx unless ctx == Undefined @__context__ end
format(matcher)
click to toggle source
# File lib/mystique/presenter.rb, line 87 def self.format(matcher) if matcher.is_a?(Symbol) __formatted_methods__ << matcher end end
format_and_present(matcher)
click to toggle source
# File lib/mystique/presenter.rb, line 105 def self.format_and_present(matcher) format_method(method) present_method(method) end
format_multiple(*matchers, &block)
click to toggle source
# File lib/mystique/presenter.rb, line 134 def self.format_multiple(*matchers, &block) matchers.each do |matcher| apply_format(matcher, &block) end end
present(matcher)
click to toggle source
# File lib/mystique/presenter.rb, line 93 def self.present(matcher) if matcher.is_a?(Symbol) __presented_methods__ << matcher end end
present_collection(matcher)
click to toggle source
# File lib/mystique/presenter.rb, line 99 def self.present_collection(matcher) if matcher.is_a?(Symbol) __presented_collections__ << matcher end end
Public Instance Methods
context()
click to toggle source
# File lib/mystique/presenter.rb, line 18 def context @__context__ end
inspect()
click to toggle source
# File lib/mystique/presenter.rb, line 28 def inspect "<#{self.class}(#{o.inspect}) context: #{context.inspect}>" end
o()
click to toggle source
# File lib/mystique/presenter.rb, line 24 def o @__object__ end
Private Instance Methods
__class_formats__()
click to toggle source
# File lib/mystique/presenter.rb, line 156 def __class_formats__ self.class.__class_formats__ end
__formats__()
click to toggle source
# File lib/mystique/presenter.rb, line 140 def __formats__ self.class.__formats__ end
__formatted_methods__()
click to toggle source
# File lib/mystique/presenter.rb, line 130 def __formatted_methods__ self.class.__formatted_methods__ end
__presented_collections__()
click to toggle source
# File lib/mystique/presenter.rb, line 126 def __presented_collections__ self.class.__presented_collections__ end
__presented_methods__()
click to toggle source
# File lib/mystique/presenter.rb, line 122 def __presented_methods__ self.class.__presented_methods__ end
__regex_formats__()
click to toggle source
# File lib/mystique/presenter.rb, line 148 def __regex_formats__ self.class.__regex_formats__ end
format(value)
click to toggle source
# File lib/mystique/presenter.rb, line 63 def format(value) result = case when __formats__.keys.include?(value) __formats__[value] when __regex_formats__.any? { |regex, _| value =~ regex} __regex_formats__.select { |regex, _| value =~ regex}.first.last when __class_formats__.any? { |klass, _| value.is_a?(klass)} __class_formats__.select { |klass, _| value.is_a?(klass)}.first.last else value end Mystique.present(Callable(result).call(value, context)) end
formatted_method?(method)
click to toggle source
# File lib/mystique/presenter.rb, line 51 def formatted_method?(method) __formatted_methods__.include?(method) end
method_missing(method, *args, &block)
click to toggle source
# File lib/mystique/presenter.rb, line 34 def method_missing(method, *args, &block) return o.send(method, *args, &block) if method.to_s.start_with?("to_") o.send(method, *args, &block).yield_self { |value| case when formatted_method?(method) format( value ) when presented_method?(method) Mystique.present(value, context: context) when presented_collection?(method) Mystique.present_collection(value, context: context, &block) else value end } end
presented_collection?(method)
click to toggle source
# File lib/mystique/presenter.rb, line 59 def presented_collection?(method) __presented_collections__.include?(method) end
presented_method?(method)
click to toggle source
# File lib/mystique/presenter.rb, line 55 def presented_method?(method) __presented_methods__.include?(method) end