Module: NoBrainer::Enum::NoBrainerExtensions
- Included in:
- NoBrainer::Enum
- Defined in:
- lib/no_brainer/document/types/enum.rb
Constant Summary collapse
- InvalidType =
NoBrainer::Error::InvalidType
Class Method Summary collapse
Instance Method Summary collapse
- #nobrainer_cast_db_to_model(value) ⇒ Object
- #nobrainer_cast_user_to_model(value) ⇒ Object
- #nobrainer_field_defined(model, attr, options = {}) ⇒ Object
- #nobrainer_field_undefined(model, attr, options = {}) ⇒ Object
Class Method Details
.method_name(value, attr, options) ⇒ Object
57 58 59 60 |
# File 'lib/no_brainer/document/types/enum.rb', line 57 def self.method_name(value, attr, ) [[:prefix] == true ? attr : [:prefix], value, [:suffix] == true ? attr : [:suffix]].compact.join("_") end |
Instance Method Details
#nobrainer_cast_db_to_model(value) ⇒ Object
14 15 16 |
# File 'lib/no_brainer/document/types/enum.rb', line 14 def nobrainer_cast_db_to_model(value) Symbol.nobrainer_cast_db_to_model(value) end |
#nobrainer_cast_user_to_model(value) ⇒ Object
10 11 12 |
# File 'lib/no_brainer/document/types/enum.rb', line 10 def nobrainer_cast_user_to_model(value) Symbol.nobrainer_cast_user_to_model(value) end |
#nobrainer_field_defined(model, attr, options = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/no_brainer/document/types/enum.rb', line 18 def nobrainer_field_defined(model, attr, ={}) NoBrainer::Document::Types.load_type_extensions(Symbol) unless [:in].present? raise "When using Enum on `#{model}.#{attr}', you must provide the `:in` option to specify values" end unless [:in].all? { |v| v.is_a?(Symbol) } raise "The `:in` option must specify symbol values" end model.inject_in_layer :enum do extend ActiveSupport::Concern const_set(:ClassMethods, Module.new) unless const_defined?(:ClassMethods) [:in].each do |value| method = NoBrainer::Enum::NoBrainerExtensions.method_name(value, attr, ) if method_defined?("#{method}?") raise "The method `#{method}' is already taken. You may specify a :prefix or :suffix option" end define_method("#{method}?") { read_attribute(attr) == value } define_method("#{method}!") { write_attribute(attr, value) } const_get(:ClassMethods).__send__(:define_method, "#{method}") { where(attr => value) } end end end |
#nobrainer_field_undefined(model, attr, options = {}) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/no_brainer/document/types/enum.rb', line 46 def nobrainer_field_undefined(model, attr, ={}) model.inject_in_layer :enum do model.fields[attr][:in].each do |value| method = NoBrainer::Enum::NoBrainerExtensions.method_name(value, attr, model.fields[attr]) remove_method("#{method}?") remove_method("#{method}!") const_get(:ClassMethods).__send__(:remove_method, "#{method}") end end end |