Sha256: e943ac05569c88d702157ce7631da72299f1f27ae9bc96b273362874b521e33d
Contents?: true
Size: 1.6 KB
Versions: 1
Compression:
Stored size: 1.6 KB
Contents
require 'active_support/concern' require 'active_support/inflector' module FlexibleEnum module Mixin extend ActiveSupport::Concern module ClassMethods def flexible_enums @flexible_enums ||= {} end def flexible_enum(attribute_name, attribute_options = {}, &config) # Methods are defined on the feature module which in turn is mixed in to the target class feature_module = Module.new do |m| extend ActiveSupport::Concern const_set :ClassMethods, Module.new def m.inspect "FlexibleEnum(#{self})" end end # The module that will hold references to value constants module_for_elements = attribute_options[:namespace] ? self.const_set(attribute_options[:namespace], Module.new) : feature_module # Read configuration elements = Configuration.load(&config).elements # Configure the target object for the given attribute configurators = [ConstantConfigurator, NameConfigurator, QuestionMethodConfigurator, SetterMethodConfigurator, ScopeConfigurator, PotentialValuesConfigurator] configurators.each do |configurator| configurator.new(feature_module, attribute_name, module_for_elements, elements).apply end # Add functionality to target inheritance chain send(:include, feature_module) flexible_enums[attribute_name] = public_send("#{attribute_name.to_s.pluralize}_by_sym") end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
flexible_enum-0.2.2 | lib/flexible_enum/mixin.rb |