module GrapeOnRails::Attributes

Constants

TYPE

Public Instance Methods

declared_attrs(attrs) click to toggle source
# File lib/grape_on_rails/attributes.rb, line 5
def declared_attrs attrs
  @declared_attrs ||= []
  @declared_attrs |= attrs
end
declared_params() click to toggle source
# File lib/grape_on_rails/attributes.rb, line 10
def declared_params
  params.permit @declared_attrs
end
undeclare_params(*params) click to toggle source
# File lib/grape_on_rails/attributes.rb, line 14
def undeclare_params *params
  @declared_attrs -= params
end

Private Instance Methods

verify(method, attrs, value) click to toggle source
# File lib/grape_on_rails/attributes.rb, line 19
def verify method, attrs, value
  attrs.each do |attr|
    is_valid = send "verify_#{method}", attr, value
    raise APIError::ValidationError, "#{attr} #{I18n.t "errors.messages.#{method}"}" unless is_valid
    instance_variable_set "@#{attr}".to_sym, params[attr]
    self.class.class_eval{attr_reader attr}
  end
end
verify_allow_blank(attr, is_allow) click to toggle source
# File lib/grape_on_rails/attributes.rb, line 44
def verify_allow_blank attr, is_allow
  params[attr].present? || is_allow
end
verify_regexp(attr, regex) click to toggle source
# File lib/grape_on_rails/attributes.rb, line 48
def verify_regexp attr, regex
  params[attr] =~ regex
end
verify_type(attr, type, param = nil) click to toggle source
# File lib/grape_on_rails/attributes.rb, line 28
def verify_type attr, type, param = nil
  return verify_type_in_array attr, type if type.is_a? Array
  return true if type == File
  param ||= params[attr]
  param.class.include GrapeOnRails::Types::Boolean if type == GrapeOnRails::Types::Boolean
  convert_method = TYPE[type.name.to_sym] || "to_#{type.name[0].downcase}"
  params[attr] = param.public_send convert_method
  true
rescue StandardError
  false
end
verify_type_in_array(attr, type) click to toggle source
# File lib/grape_on_rails/attributes.rb, line 40
def verify_type_in_array attr, type
  params[attr].all?{|p| verify_type(attr, type.to_a.first, param: p)}
end