Sha256: bac98a01ee8f0f4a6fa6e537a89f4792af11cf7b293d8b20417afad4eb27af00
Contents?: true
Size: 1.28 KB
Versions: 2
Compression:
Stored size: 1.28 KB
Contents
module EnumIsh class Builder class ActiveRecord < Builder private def define_default_value(attr, enum, config) method = "_enum_ish_init_#{attr}".to_sym @klass.class_eval do after_initialize method define_method method do if respond_to?(attr) && public_send(attr).nil? default = if config[:default].kind_of?(Proc) instance_exec(&config[:default]) else config[:default] end public_send("#{attr}=", default) end end end end def define_accessor(attr, enum, config) method = "#{attr}_raw" @klass.class_eval do define_method method do read_attribute(attr) end define_method "#{attr}" do enum.invert[read_attribute(attr)] end define_method "#{attr}=" do |value| write_attribute(attr, enum[value]) end end end def define_scope(attr, enum, config) method = "with_#{attr}" @klass.class_eval do scope method, ->(value) { where(attr => (enum[value] || value)) } end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
enum_ish-1.1.1 | lib/enum_ish/builder/active_record.rb |
enum_ish-1.1.0 | lib/enum_ish/builder/active_record.rb |