lib/enum_ish/enum.rb in enum_ish-1.5.1 vs lib/enum_ish/enum.rb in enum_ish-1.6.0
- old
+ new
@@ -1,19 +1,52 @@
# frozen_string_literal: true
module EnumIsh
class Enum
+ class_attribute :features
+ self.features = [:text, :options, :predicate, :default, :accessor, :validate, :scope]
+
attr_accessor :name
attr_accessor :mapping
attr_accessor :setting
def initialize(name, mapping, setting)
@name = name
@mapping = init_mapping(mapping)
- @setting = init_setting(setting)
+ @setting = setting
end
+ features.each do |feature|
+ define_method feature do
+ setting.fetch(feature)
+ end
+ end
+
+ def use?(feature)
+ @setting.key?(feature)
+ end
+
+ def features
+ self.class.features.select { |feature| use?(feature) }
+ end
+
+ def values
+ if use?(:accessor)
+ @mapping.keys
+ else
+ @mapping.values
+ end
+ end
+
+ def value(key)
+ if use?(:accessor)
+ key
+ else
+ @mapping[key]
+ end
+ end
+
def to_raw(value)
if value.is_a?(Array)
value.map { |v| @mapping.fetch(v, v) }
else
@mapping.fetch(value, value)
@@ -27,10 +60,39 @@
else
inverted[value]
end
end
+ def text_method
+ "#{Config.text_prefix}#{@name}#{Config.text_suffix}"
+ end
+
+ def options_method
+ "#{Config.options_prefix}#{@name}#{Config.options_suffix}"
+ end
+
+ def predicate_method(key)
+ key = key.to_s.tr('.', '_')
+ if predicate.is_a?(Hash) && predicate[:prefix] == false
+ "#{key}?"
+ else
+ "#{@name}_#{key}?"
+ end
+ end
+
+ def raw_method
+ "#{Config.raw_prefix}#{@name}#{Config.raw_suffix}"
+ end
+
+ def scope_method(type = nil)
+ if type == :negative
+ "#{Config.scope_prefix}#{@name}_not#{Config.scope_suffix}"
+ else
+ "#{Config.scope_prefix}#{@name}#{Config.scope_suffix}"
+ end
+ end
+
private
def init_mapping(mapping)
if mapping.is_a?(Array)
mapping.map do |v|
@@ -39,15 +101,8 @@
[k, v]
end.to_h
else
mapping
end
- end
-
- def init_setting(setting)
- [:text, :options].each do |key|
- setting[key] = true unless setting.key?(key)
- end
- setting
end
end
end