Sha256: afa02ee5624bf991ecf8b15358e67004008832750904aad6144b7177f98b1c31
Contents?: true
Size: 1.6 KB
Versions: 65
Compression:
Stored size: 1.6 KB
Contents
# frozen_string_literal: true class ReeMapper::Field attr_reader :type, :name, :from, :doc, :optional, :null, :roles, :default, :name_as_str, :name_as_instance_var_name, :from_as_str, :fields_filter NO_DEFAULT = Object.new.freeze contract( Any, Nilor[Symbol], Kwargs[ from: Nilor[Symbol], doc: Nilor[String], optional: Bool, null: Bool, role: Nilor[ArrayOf[Symbol], Symbol], default: Any, only: Nilor[ReeMapper::FilterFieldsContract], except: Nilor[ReeMapper::FilterFieldsContract] ] => Any ).throws(ArgumentError) def initialize(type, name = nil, from: nil, doc: nil, optional: false, null: false, role: nil, default: NO_DEFAULT, only: nil, except: nil) @type = type @name = name @from = from || name @doc = doc @optional = optional @null = null @roles = Array(role) @default = default @fields_filter = ReeMapper::FieldsFilter.build(only: only, except: except) @name_as_str = @name.to_s @name_as_instance_var_name = :"@#{@name}" @from_as_str = @from.to_s raise ArgumentError, 'required fields do not support defaults' if has_default? && !optional end contract None => Bool def has_default? default != NO_DEFAULT end contract Nilor[Symbol, ArrayOf[Symbol]] => Bool def has_role?(role) return true if roles.empty? return false if role.nil? if role.is_a?(Array) role.any? { roles.include?(_1) } else roles.include?(role) end end end
Version data entries
65 entries across 65 versions & 1 rubygems