Class: ActiveRecord::Bixformer::Attribute::Enumerize

Inherits:
Base
  • Object
show all
Defined in:
lib/activerecord-bixformer/attribute/enumerize.rb

Instance Attribute Summary

Attributes inherited from Base

#model, #name, #options

Instance Method Summary collapse

Methods inherited from Base

#initialize, #should_be_included

Constructor Details

This class inherits a constructor from ActiveRecord::Bixformer::Attribute::Base

Instance Method Details

#export(record) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/activerecord-bixformer/attribute/enumerize.rb', line 5

def export(record)
  record_attribute_value = record_attribute_value(record).to_s

  @model.activerecord_constant.__send__(@name).options.find do |text, key|
    key == record_attribute_value
  end&.first
end

#import(value) ⇒ Object



13
14
15
16
17
18
# File 'lib/activerecord-bixformer/attribute/enumerize.rb', line 13

def import(value)
  return nil if value.blank?

  @model.activerecord_constant.__send__(@name).options.to_h[value.strip] or
    raise ::ActiveRecord::Bixformer::AttributeError.new(self, value) if @options[:raise]
end