Module: ClassComposer::Generator::ClassMethods
- Defined in:
- lib/class_composer/generator.rb
Constant Summary collapse
- COMPOSER_VALIDATE_METHOD_NAME =
->(name) { :"__composer_#{name}_is_valid__?" }
- COMPOSER_ASSIGNED_ATTR_NAME =
->(name) { :"@__composer_#{name}_value_assigned__" }
- COMPOSER_ASSIGNED_ARRAY_METHODS =
->(name) { :"@__composer_#{name}_array_methods_set__" }
Instance Method Summary collapse
- #__composer_array_proc__(name:, validator:, allowed:, params:) ⇒ Object
-
#__composer_assignment__(name:, params:, allowed:, validator:, array_proc:, validation_error_klass:, error_klass:) ⇒ Object
create assignment method for the incoming name.
-
#__composer_retrieval__(name:, default:, array_proc:) ⇒ Object
retrieve the value for the name – Or return the default value.
- #__composer_validate_options__!(name:, validate_proc:, default:, params: {}, validation_error_klass:, error_klass:) ⇒ Object
-
#__composer_validator_proc__(validator:, allowed:, name:, error_klass:) ⇒ Object
create validator method for incoming name.
- #add_composer(name, allowed:, accessor: true, validator: ->(_) { true }, validation_error_klass: ::ClassComposer::ValidatorError, error_klass: ::ClassComposer::Error, **params) ⇒ Object
Instance Method Details
#__composer_array_proc__(name:, validator:, allowed:, params:) ⇒ Object
48 49 50 51 52 |
# File 'lib/class_composer/generator.rb', line 48 def __composer_array_proc__(name:, validator:, allowed:, params:) Proc.new do |value, _itself| _itself.send(:"#{name}=", value) end end |
#__composer_assignment__(name:, params:, allowed:, validator:, array_proc:, validation_error_klass:, error_klass:) ⇒ Object
create assignment method for the incoming name
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/class_composer/generator.rb', line 55 def __composer_assignment__(name:, params:, allowed:, validator:, array_proc:, validation_error_klass:, error_klass:) define_method(:"#{name}=") do |value| is_valid = validator.(value) if is_valid instance_variable_set(COMPOSER_ASSIGNED_ATTR_NAME.(name), true) instance_variable_set(:"@#{name}", value) else = ["#{self.class}.#{name} failed validation. #{name} is expected to be #{allowed}."] << (params[:invalid_message].is_a?(Proc) ? params[:invalid_message].(value) : params[:invalid_message].to_s) if value.is_a?(Array) # we assigned the array value...pop it from the array # must be done after the message is created so that failing value can get passed appropriately value.pop end raise validation_error_klass, .compact.join(" ") end if value.is_a?(Array) && !value.instance_variable_get(COMPOSER_ASSIGNED_ARRAY_METHODS.(name)) _itself = itself value.define_singleton_method(:<<) do |val| array_proc.(super(val), _itself) end value.instance_variable_set(COMPOSER_ASSIGNED_ARRAY_METHODS.(name), true) end value end end |
#__composer_retrieval__(name:, default:, array_proc:) ⇒ Object
retrieve the value for the name – Or return the default value
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/class_composer/generator.rb', line 87 def __composer_retrieval__(name:, default:, array_proc:) define_method(:"#{name}") do value = instance_variable_get(:"@#{name}") return value if instance_variable_get(COMPOSER_ASSIGNED_ATTR_NAME.(name)) if default.is_a?(Array) && !default.instance_variable_get(COMPOSER_ASSIGNED_ARRAY_METHODS.(name)) _itself = itself default.define_singleton_method(:<<) do |value| array_proc.(super(value), _itself) end default.instance_variable_set(COMPOSER_ASSIGNED_ARRAY_METHODS.(name), true) end default == ClassComposer::DefaultObject ? ClassComposer::DefaultObject.value : default end end |
#__composer_validate_options__!(name:, validate_proc:, default:, params: {}, validation_error_klass:, error_klass:) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/class_composer/generator.rb', line 38 def (name:, validate_proc:, default:, params: {}, validation_error_klass:, error_klass:) unless validate_proc.(default) raise validation_error_klass, "Default value [#{default}] for #{self.class}.#{name} is not valid" end if instance_methods.include?(name.to_sym) raise error_klass, "[#{name}] is already defined. Ensure composer names are all uniq and do not class with class instance methods" end end |
#__composer_validator_proc__(validator:, allowed:, name:, error_klass:) ⇒ Object
create validator method for incoming name
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/class_composer/generator.rb', line 105 def __composer_validator_proc__(validator:, allowed:, name:, error_klass:) if validator && !validator.is_a?(Proc) raise error_klass, "Expected validator to be a Proc. Received [#{validator.class}]" end # Proc will validate the entire attribute -- Full assignment must occur before validate is called Proc.new do |value| begin allow = if allowed.is_a?(Array) allowed.include?(value.class) else allowed == value.class end # order is important -- Do not run validator if it is the default object # Default object will likely raise an error if there is a custom validator (allowed.include?(ClassComposer::DefaultObject) && value == ClassComposer::DefaultObject) || (allow && validator.(value)) rescue StandardError => e raise error_klass, "#{e} occured during validation for value [#{value}]. Check custom validator for #{name}" end end end |
#add_composer(name, allowed:, accessor: true, validator: ->(_) { true }, validation_error_klass: ::ClassComposer::ValidatorError, error_klass: ::ClassComposer::Error, **params) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/class_composer/generator.rb', line 16 def add_composer(name, allowed:, accessor: true, validator: ->(_) { true }, validation_error_klass: ::ClassComposer::ValidatorError, error_klass: ::ClassComposer::Error, **params) default = if params.has_key?(:default) params[:default] else if allowed.is_a?(Array) allowed << ClassComposer::DefaultObject else allowed = [allowed, ClassComposer::DefaultObject] end ClassComposer::DefaultObject end allowed.include?(ClassComposer::DefaultObject) validate_proc = __composer_validator_proc__(validator: validator, allowed: allowed, name: name, error_klass: error_klass) (name: name, validate_proc: validate_proc, default: default, validation_error_klass: validation_error_klass, error_klass: error_klass) array_proc = __composer_array_proc__(name: name, validator: validator, allowed: allowed, params: params) __composer_assignment__(name: name, allowed: allowed, params: params, validator: validate_proc, array_proc: array_proc, validation_error_klass: validation_error_klass, error_klass: error_klass) __composer_retrieval__(name: name, default: default, array_proc: array_proc) end |