module RailsRbs::FollowsRule

Public Instance Methods

default_owner() click to toggle source
# File lib/rails_rbs/follows_rule.rb, line 48
def default_owner
  owner_field = "#{RailsRbs.owned_by.to_s.underscore}".to_sym
  owned_by = self.send(owner_field)
  self.send("#{owner_field}=", self.rule_set.send(owner_field)) if owned_by.nil? && self.rule_set.present?
end
default_rule(type) click to toggle source
# File lib/rails_rbs/follows_rule.rb, line 58
def default_rule(type)
  return unless supported_defaults.include?(type)
  class_eval { include("RailsRbs::Rules::#{type.to_s.camelize}".constantize) }
end
filter_objects(association) click to toggle source
# File lib/rails_rbs/follows_rule.rb, line 27
def filter_objects(association)
  # association.where for example
  raise NotImplementedError.new("This instance of #{self.class.to_s} fails to define a find_objects method though it claims to be a rule...")
end
follows_rule?(*objects) click to toggle source

Defined for documentation, and prevents user from not defining a rule class correctly somehow…

# File lib/rails_rbs/follows_rule.rb, line 24
def follows_rule?(*objects)
  raise NotImplementedError.new("This instance of #{self.class.to_s} fails to define a follows_rule? method though it claims to be a rule...")
end
force_type(object) click to toggle source

Coerce the provided object to the rule's enforced_type. If the enforced type is a string integer or float then to_s, to_i or to_f are used, otherwise we attempt to call to_<enforced_type> on the provided object. @param object The object to coerce

# File lib/rails_rbs/follows_rule.rb, line 35
def force_type(object)
  return unless self.respond_to?(:enforced_type)
  enforced_type_string = self.enforced_type.to_s.downcase
  if enforced_type_string.to_s.downcase.to_sym == :string
    object.to_s
  elsif enforced_type_string.to_s.downcase =~ /\Aint(?!iger)\z/
    object.to_i
  elsif enforced_type_string.to_s.downcase.to_sym == :float
    object.to_f
  else
    object.send("to_#{enforced_type_string}")
  end
end
supported_defaults() click to toggle source
# File lib/rails_rbs/follows_rule.rb, line 55
def supported_defaults
  %i[equality date_range location]
end