Class: PaperTrail::Human::Adapters::Resolvers::Enum

Inherits:
Object
  • Object
show all
Includes:
Ports::Resolver
Defined in:
lib/paper_trail/human/adapters/resolvers/enum.rb

Instance Method Summary collapse

Constructor Details

#initialize(class_name: nil, method: :label, mapping: nil, from_model: nil, **options) ⇒ Enum

Returns a new instance of Enum.



10
11
12
13
14
15
16
17
# File 'lib/paper_trail/human/adapters/resolvers/enum.rb', line 10

def initialize(class_name: nil, method: :label, mapping: nil, from_model: nil, **options)
  @class_name = class_name || options[:class].to_s
  @method = method
  @mapping = mapping
  @from_model = from_model
  @field = options[:field]&.to_s
  @labels = options[:labels]
end

Instance Method Details

#resolve(value) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/paper_trail/human/adapters/resolvers/enum.rb', line 19

def resolve(value)
  return resolve_from_model(value) if @from_model
  return @mapping[value] || value if @mapping

  klass = Object.const_get(@class_name)
  if klass.respond_to?(@method)
    klass.public_send(@method, value) || value
  else
    value
  end
rescue NameError
  value
end