module VariaModel::ClassMethods
Constants
- ASSIGNMENT_MODES
Public Instance Methods
assignment_mode()
click to toggle source
@return [Symbol]
# File lib/varia_model.rb, line 25 def assignment_mode @assignment_mode ||= :whitelist end
attribute(name, options = {})
click to toggle source
@param [#to_s] name @option options [Symbol, Array<Symbol>] :type @option options [Buff::Boolean] :required @option options [Object] :default @option options [Proc] :coerce
# File lib/varia_model.rb, line 49 def attribute(name, options = {}) name = name.to_s options[:type] = Array(options[:type]) options[:required] ||= false register_attribute(name, options) define_mimic_methods(name, options) end
attributes()
click to toggle source
@return [VariaModel::Attributes]
# File lib/varia_model.rb, line 15 def attributes @attributes ||= Attributes.new end
set_assignment_mode(mode)
click to toggle source
Set the attribute mass assignment mode
* :whitelist - only attributes defined on the class will have values set * :carefree - values will be set for attributes that are not explicitly defined in the class definition
@param [Symbol] mode
an assignment mode to use @see {ASSIGNMENT_MODES}
# File lib/varia_model.rb, line 36 def set_assignment_mode(mode) unless ASSIGNMENT_MODES.include?(mode) raise ArgumentError, "unknown assignment mode: #{mode}" end @assignment_mode = mode end
validate_kind_of(types, model, key)
click to toggle source
@param [Constant, Array<Constant>] types @param [VariaModel] model @param [String] key
@return [Array]
# File lib/varia_model.rb, line 70 def validate_kind_of(types, model, key) errors = Array.new types = types.uniq matches = false types.each do |type| if model.get_attribute(key).is_a?(type) matches = true break end end if matches [ :ok, "" ] else types_msg = types.collect { |type| "'#{type}'" } [ :error, "Expected attribute: '#{key}' to be a type of: #{types_msg.join(', ')}" ] end end
validate_required(model, key)
click to toggle source
Validate that the attribute on the given model has a non-nil value assigned
@param [VariaModel] model @param [String] key
@return [Array]
# File lib/varia_model.rb, line 96 def validate_required(model, key) if model.get_attribute(key).nil? [ :error, "A value is required for attribute: '#{key}'" ] else [ :ok, "" ] end end
validations()
click to toggle source
@return [Hashie::Mash]
# File lib/varia_model.rb, line 20 def validations @validations ||= Hashie::Mash.new end
validations_for(name)
click to toggle source
@param [String] name
@return [Array]
# File lib/varia_model.rb, line 61 def validations_for(name) self.validations[name] ||= Array.new end
Private Instance Methods
define_mimic_methods(name, options = {})
click to toggle source
# File lib/varia_model.rb, line 136 def define_mimic_methods(name, options = {}) fun_name = name.split('.').first class_eval do define_method(fun_name) do _attributes_[fun_name] end define_method("#{fun_name}=") do |value| value = if options[:coerce].is_a?(Proc) options[:coerce].call(value) else value end _attributes_[fun_name] = value end end end
register_attribute(name, options = {})
click to toggle source
# File lib/varia_model.rb, line 106 def register_attribute(name, options = {}) if options[:type] && options[:type].any? unless options[:required] options[:type] << NilClass end register_validation(name, lambda { |object, key| validate_kind_of(options[:type], object, key) }) end if options[:required] register_validation(name, lambda { |object, key| validate_required(object, key) }) end class_eval do new_attributes = Attributes.from_dotted_path(name, options[:default]) self.attributes.merge!(new_attributes) if options[:coerce].is_a?(Proc) register_coercion(name, options[:coerce]) end end end
register_coercion(name, fun)
click to toggle source
# File lib/varia_model.rb, line 132 def register_coercion(name, fun) self.attributes.container(name).set_coercion(name.split('.').last, fun) end
register_validation(name, fun)
click to toggle source
# File lib/varia_model.rb, line 128 def register_validation(name, fun) self.validations[name] = (self.validations_for(name) << fun) end