lib/str_enum.rb in str_enum-0.1.2 vs lib/str_enum.rb in str_enum-0.1.3
- old
+ new
@@ -1,14 +1,15 @@
require "str_enum/version"
require "active_support"
module StrEnum
- def str_enum(column, values, validate: true, scopes: true, accessor_methods: true, prefix: false)
+ def str_enum(column, values, validate: true, scopes: true, accessor_methods: true, prefix: false, suffix: false)
values = values.map(&:to_s)
validates column, presence: true, inclusion: {in: values} if validate
values.each do |value|
prefix = column if prefix == true
- method_name = prefix ? "#{prefix}_#{value}" : value
+ suffix = column if suffix == true
+ method_name = [prefix, value, suffix].select { |v| v }.join("_")
scope method_name, -> { where(column => value) } if scopes && !respond_to?(method_name)
if accessor_methods && !method_defined?("#{method_name}?")
define_method "#{method_name}?" do
read_attribute(column) == value
end