Class: EnumIsh::Enum
- Inherits:
-
Object
- Object
- EnumIsh::Enum
- Defined in:
- lib/enum_ish/enum.rb
Instance Attribute Summary collapse
-
#mapping ⇒ Object
Returns the value of attribute mapping.
-
#name ⇒ Object
Returns the value of attribute name.
-
#setting ⇒ Object
Returns the value of attribute setting.
Instance Method Summary collapse
- #features ⇒ Object
-
#initialize(name, mapping, setting) ⇒ Enum
constructor
A new instance of Enum.
- #options_method ⇒ Object
- #predicate_method(key) ⇒ Object
- #raw_method ⇒ Object
- #scope_method(type = nil) ⇒ Object
- #text_method ⇒ Object
- #to_raw(value) ⇒ Object
- #to_sym(value) ⇒ Object
- #use?(feature) ⇒ Boolean
- #value(key) ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(name, mapping, setting) ⇒ Enum
Returns a new instance of Enum.
12 13 14 15 16 |
# File 'lib/enum_ish/enum.rb', line 12 def initialize(name, mapping, setting) @name = name @mapping = init_mapping(mapping) @setting = setting end |
Instance Attribute Details
#mapping ⇒ Object
Returns the value of attribute mapping.
9 10 11 |
# File 'lib/enum_ish/enum.rb', line 9 def mapping @mapping end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/enum_ish/enum.rb', line 8 def name @name end |
#setting ⇒ Object
Returns the value of attribute setting.
10 11 12 |
# File 'lib/enum_ish/enum.rb', line 10 def setting @setting end |
Instance Method Details
#features ⇒ Object
28 29 30 |
# File 'lib/enum_ish/enum.rb', line 28 def features self.class.features.select { |feature| use?(feature) } end |
#options_method ⇒ Object
69 70 71 |
# File 'lib/enum_ish/enum.rb', line 69 def "#{Config.}#{@name}#{Config.}" end |
#predicate_method(key) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/enum_ish/enum.rb', line 73 def predicate_method(key) key = key.to_s.tr('.', '_') if predicate.is_a?(Hash) && predicate[:prefix] == false "#{key}?" else "#{@name}_#{key}?" end end |
#raw_method ⇒ Object
82 83 84 |
# File 'lib/enum_ish/enum.rb', line 82 def raw_method "#{Config.raw_prefix}#{@name}#{Config.raw_suffix}" end |
#scope_method(type = nil) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/enum_ish/enum.rb', line 86 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 |
#text_method ⇒ Object
65 66 67 |
# File 'lib/enum_ish/enum.rb', line 65 def text_method "#{Config.text_prefix}#{@name}#{Config.text_suffix}" end |
#to_raw(value) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/enum_ish/enum.rb', line 48 def to_raw(value) if value.is_a?(Array) value.map { |v| @mapping.fetch(v, v) } else @mapping.fetch(value, value) end end |
#to_sym(value) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/enum_ish/enum.rb', line 56 def to_sym(value) inverted = @mapping.invert if value.is_a?(Array) value.map { |v| inverted[v] } else inverted[value] end end |
#use?(feature) ⇒ Boolean
24 25 26 |
# File 'lib/enum_ish/enum.rb', line 24 def use?(feature) @setting.key?(feature) end |
#value(key) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/enum_ish/enum.rb', line 40 def value(key) if use?(:accessor) key else @mapping[key] end end |
#values ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/enum_ish/enum.rb', line 32 def values if use?(:accessor) @mapping.keys else @mapping.values end end |