Class: EnumIsh::Enum

Inherits:
Object
  • Object
show all
Defined in:
lib/enum_ish/enum.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#mappingObject

Returns the value of attribute mapping.



9
10
11
# File 'lib/enum_ish/enum.rb', line 9

def mapping
  @mapping
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/enum_ish/enum.rb', line 8

def name
  @name
end

#settingObject

Returns the value of attribute setting.



10
11
12
# File 'lib/enum_ish/enum.rb', line 10

def setting
  @setting
end

Instance Method Details

#featuresObject



28
29
30
# File 'lib/enum_ish/enum.rb', line 28

def features
  self.class.features.select { |feature| use?(feature) }
end

#options_methodObject



69
70
71
# File 'lib/enum_ish/enum.rb', line 69

def options_method
  "#{Config.options_prefix}#{@name}#{Config.options_suffix}"
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_methodObject



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_methodObject



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

Returns:

  • (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

#valuesObject



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