Class: ReeMapper::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb

Constant Summary collapse

NO_DEFAULT =
Object.new.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, name = nil, from: nil, doc: nil, optional: false, null: false, role: nil, default: NO_DEFAULT, only: nil, except: nil, location: nil) ⇒ Field

Returns a new instance of Field.

Raises:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb', line 25

def initialize(type, name = nil, from: nil, doc: nil, optional: false, null: false, role: nil, default: NO_DEFAULT,
               only: nil, except: nil, location: 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, except)

  @name_as_str               = @name.to_s
  @name_as_instance_var_name = :"@#{@name}"
  @from_as_str               = @from.to_s

  @location = location
  if @location
    @location = @location
      .sub(Ree.root_dir, ".")
      .sub(/:in.+/, "")
  end

  raise ArgumentError, 'required fields do not support defaults' if has_default? && !optional
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



4
5
6
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb', line 4

def default
  @default
end

#docObject (readonly)

Returns the value of attribute doc.



4
5
6
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb', line 4

def doc
  @doc
end

#fields_filterObject (readonly)

Returns the value of attribute fields_filter.



4
5
6
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb', line 4

def fields_filter
  @fields_filter
end

#fromObject (readonly)

Returns the value of attribute from.



4
5
6
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb', line 4

def from
  @from
end

#from_as_strObject (readonly)

Returns the value of attribute from_as_str.



4
5
6
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb', line 4

def from_as_str
  @from_as_str
end

#locationObject (readonly)

Returns the value of attribute location.



4
5
6
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb', line 4

def location
  @location
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb', line 4

def name
  @name
end

#name_as_instance_var_nameObject (readonly)

Returns the value of attribute name_as_instance_var_name.



4
5
6
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb', line 4

def name_as_instance_var_name
  @name_as_instance_var_name
end

#name_as_strObject (readonly)

Returns the value of attribute name_as_str.



4
5
6
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb', line 4

def name_as_str
  @name_as_str
end

#nullObject (readonly)

Returns the value of attribute null.



4
5
6
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb', line 4

def null
  @null
end

#optionalObject (readonly)

Returns the value of attribute optional.



4
5
6
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb', line 4

def optional
  @optional
end

#rolesObject (readonly)

Returns the value of attribute roles.



4
5
6
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb', line 4

def roles
  @roles
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb', line 4

def type
  @type
end

Instance Method Details

#has_default?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb', line 53

def has_default?
  default != NO_DEFAULT
end

#has_role?(role) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb', line 58

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