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

Version Path
ree_lib-1.0.86 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb
ree_lib-1.0.85 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb
ree_lib-1.0.84 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb
ree_lib-1.0.83 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb
ree_lib-1.0.82 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb
ree_lib-1.0.81 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb
ree_lib-1.0.80 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb
ree_lib-1.0.79 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb
ree_lib-1.0.78 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb
ree_lib-1.0.77 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb
ree_lib-1.0.76 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb
ree_lib-1.0.75 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb
ree_lib-1.0.74 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb
ree_lib-1.0.73 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb
ree_lib-1.0.72 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb
ree_lib-1.0.71 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb
ree_lib-1.0.70 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb
ree_lib-1.0.69 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb
ree_lib-1.0.68 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb
ree_lib-1.0.67 lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb