Class: Torque::PostgreSQL::Attributes::Builder::Enum
- Inherits:
-
Object
- Object
- Torque::PostgreSQL::Attributes::Builder::Enum
- Defined in:
- lib/torque/postgresql/attributes/builder/enum.rb
Constant Summary collapse
- VALID_TYPES =
i[enum enum_set].freeze
- FN =
'::Torque::PostgreSQL::FN'
Instance Attribute Summary collapse
-
#attribute ⇒ Object
Returns the value of attribute attribute.
-
#instance_module ⇒ Object
Returns the value of attribute instance_module.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#klass_module ⇒ Object
Returns the value of attribute klass_module.
-
#options ⇒ Object
Returns the value of attribute options.
-
#subtype ⇒ Object
Returns the value of attribute subtype.
-
#values ⇒ Object
Returns the value of attribute values.
Instance Method Summary collapse
-
#build ⇒ Object
Create all methods needed.
-
#conflicting? ⇒ Boolean
Check if any of the methods that will be created get in conflict with the base class methods.
-
#initialize(klass, attribute, options) ⇒ Enum
constructor
Start a new builder of methods for enum values on ActiveRecord::Base.
-
#set_features? ⇒ Boolean
Check if it’s building the methods for sets.
-
#values_methods ⇒ Object
Get the list of methods based on enum values.
Constructor Details
#initialize(klass, attribute, options) ⇒ Enum
Start a new builder of methods for enum values on ActiveRecord::Base
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 15 def initialize(klass, attribute, ) @klass = klass @attribute = attribute.to_s @subtype = klass.attribute_types[@attribute] = raise Interrupt unless subtype.respond_to?(:klass) @values = subtype.klass.values if [:only] @values &= Array([:only]).map(&:to_s) end if [:except] @values -= Array([:except]).map(&:to_s) end end |
Instance Attribute Details
#attribute ⇒ Object
Returns the value of attribute attribute.
11 12 13 |
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 11 def attribute @attribute end |
#instance_module ⇒ Object
Returns the value of attribute instance_module.
11 12 13 |
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 11 def instance_module @instance_module end |
#klass ⇒ Object
Returns the value of attribute klass.
11 12 13 |
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 11 def klass @klass end |
#klass_module ⇒ Object
Returns the value of attribute klass_module.
11 12 13 |
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 11 def klass_module @klass_module end |
#options ⇒ Object
Returns the value of attribute options.
11 12 13 |
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 11 def end |
#subtype ⇒ Object
Returns the value of attribute subtype.
11 12 13 |
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 11 def subtype @subtype end |
#values ⇒ Object
Returns the value of attribute values.
11 12 13 |
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 11 def values @values end |
Instance Method Details
#build ⇒ Object
Create all methods needed
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 92 def build @klass_module = Module.new @instance_module = Module.new plural stringify all_values set_scopes if set_features? klass.extend klass_module klass.include instance_module end |
#conflicting? ⇒ Boolean
Check if any of the methods that will be created get in conflict with the base class methods
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 63 def conflicting? return if [:force] == true attributes = attribute.pluralize dangerous?(attributes, true) dangerous?("#{attributes}_keys", true) dangerous?("#{attributes}_texts", true) dangerous?("#{attributes}_options", true) dangerous?("#{attribute}_text") if set_features? dangerous?("has_#{attributes}", true) dangerous?("has_any_#{attributes}", true) end values_methods.each do |attr, (scope, ask, bang, *)| dangerous?(scope, true) dangerous?(bang) dangerous?(ask) end rescue Interrupt => err raise ArgumentError, " Enum \#{subtype.name} was not able to generate requested\n methods because the method \#{err} already exists in\n \#{klass.name}.\n MSG\nend\n".squish |
#set_features? ⇒ Boolean
Check if it’s building the methods for sets
57 58 59 |
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 57 def set_features? [:set_features].present? end |
#values_methods ⇒ Object
Get the list of methods based on enum values
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 34 def values_methods return @values_methods if defined?(@values_methods) prefix = .fetch(:prefix, nil) suffix = .fetch(:suffix, nil) prefix = attribute if prefix == true suffix = attribute if suffix == true base = [prefix, '%s', suffix].compact.join('_') @values_methods = begin values.map do |val| key = val.downcase.tr('- ', '__') scope = base % key ask = scope + '?' bang = scope + '!' [key, [scope, ask, bang, val]] end.to_h end end |