Class: Livery::Presenter

Inherits:
Object
  • Object
show all
Defined in:
lib/livery/presenter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, options = {}) ⇒ Presenter

Returns a new instance of Presenter.



5
6
7
8
9
10
11
# File 'lib/livery/presenter.rb', line 5

def initialize(resource, options = {})
  @resource = resource
  options.each do |k, v|
    raise ArgumentError, "cannot initialize `resource` variable" if k.to_s == "resource"
    instance_variable_set("@#{k}", v)
  end
end

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



3
4
5
# File 'lib/livery/presenter.rb', line 3

def resource
  @resource
end

Class Method Details

.formatted_datetime(*names) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/livery/presenter.rb', line 14

def formatted_datetime(*names)
  names.each do |name|
    define_method name do
      value = @resource.public_send(name)
      I18n.l(value)
    end
  end
end

.method_added(sym) ⇒ Object



23
24
25
# File 'lib/livery/presenter.rb', line 23

def method_added(sym)
  raise "can't override resource on a subclass of Presenter" if sym == :resource
end

.presenter_association(*names, source: nil, namespace: nil) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/livery/presenter.rb', line 27

def presenter_association(*names, source: nil, namespace: nil)
  raise ArgumentError, '`source` must be a block' unless source.nil? || source.is_a?(Proc)

  names.each do |name|
    instance_variable_name = "@#{name}".to_sym

    define_method name do
      return instance_variable_get(instance_variable_name) if instance_variable_defined?(instance_variable_name)

      association_resource = source.nil? ? @resource.public_send(name) : @resource.instance_exec(&source)

      association_presenter = Presenter.to_presenter(association_resource, namespace: namespace)

      instance_variable_set(instance_variable_name, association_presenter)
      association_presenter
    end
  end
end

.presenterize(klass, namespace: nil) ⇒ Object



46
47
48
49
# File 'lib/livery/presenter.rb', line 46

def presenterize(klass, namespace: nil)
  presenter_klass_string = [namespace, "#{klass}Presenter"].compact.join('::')
  ActiveSupport::Inflector.constantize(presenter_klass_string)
end

.resource(sym) ⇒ Object



51
52
53
54
55
# File 'lib/livery/presenter.rb', line 51

def resource(sym)
  define_method sym do
    @resource
  end
end

.to_presenter(object, namespace: nil) ⇒ Object



57
58
59
60
# File 'lib/livery/presenter.rb', line 57

def to_presenter(object, namespace: nil)
  return object.map { |o| to_presenter_single(o, namespace: namespace) } if object.is_a?(Enumerable)
  to_presenter_single(object, namespace: namespace)
end

Instance Method Details

#t(*args) ⇒ Object



77
78
79
# File 'lib/livery/presenter.rb', line 77

def t(*args)
  I18n.t(resolve_i18n_path(args.shift), *args)
end

#t!(*args) ⇒ Object



81
82
83
# File 'lib/livery/presenter.rb', line 81

def t!(*args)
  I18n.t!(resolve_i18n_path(args.shift), *args)
end

#to_modelObject



85
86
87
# File 'lib/livery/presenter.rb', line 85

def to_model
  raise 'Presenter objects should not be used for forms. Call .resource on this Presenter'
end

#to_param(*args) ⇒ Object



89
90
91
# File 'lib/livery/presenter.rb', line 89

def to_param(*args)
  @resource.to_param(*args)
end