Class: Environmentor::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/environmentor/attribute.rb

Defined Under Namespace

Classes: ValidationError, ValidationErrors

Constant Summary collapse

Absent =
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, namespace_chain, type: :string, required: true, default: Absent, description: nil, help: nil, mappers: {}) ⇒ Attribute

Returns a new instance of Attribute.

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
# File 'lib/environmentor/attribute.rb', line 50

def initialize(name, namespace_chain, type: :string, required: true, default: Absent, description: nil, help: nil, mappers: {})
  raise ArgumentError, "#{type.inspect} isn't a valid type" unless type_coercer.valid_type?(type)
  @name = name
  @namespace_chain = namespace_chain
  @type = type
  @required = required
  @default = default
  @description = description || help
  @mappers_opts = mappers
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



46
47
48
# File 'lib/environmentor/attribute.rb', line 46

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description.



46
47
48
# File 'lib/environmentor/attribute.rb', line 46

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



46
47
48
# File 'lib/environmentor/attribute.rb', line 46

def name
  @name
end

#namespace_chainObject (readonly)

Returns the value of attribute namespace_chain.



46
47
48
# File 'lib/environmentor/attribute.rb', line 46

def namespace_chain
  @namespace_chain
end

#requiredObject (readonly) Also known as: required?

Returns the value of attribute required.



46
47
48
# File 'lib/environmentor/attribute.rb', line 46

def required
  @required
end

#typeObject (readonly)

Returns the value of attribute type.



46
47
48
# File 'lib/environmentor/attribute.rb', line 46

def type
  @type
end

#type_coercer=(value) ⇒ Object

Sets the attribute type_coercer

Parameters:

  • value

    the value to set the attribute type_coercer to.



48
49
50
# File 'lib/environmentor/attribute.rb', line 48

def type_coercer=(value)
  @type_coercer = value
end

Instance Method Details

#clear_cache!Object



108
109
110
# File 'lib/environmentor/attribute.rb', line 108

def clear_cache!
  remove_instance_variable(:@value)
end

#full_nameObject



63
64
65
# File 'lib/environmentor/attribute.rb', line 63

def full_name
  (namespace_chain.map(&:name).compact << name).join('.')
end

#get_from_mapper(mapper, **opts) ⇒ Object



88
89
90
91
# File 'lib/environmentor/attribute.rb', line 88

def get_from_mapper(mapper, **opts)
  str_value = mapper.value_for_attribute(self, **opts) or return nil
  type_coercer.coerce_to(type, str_value)
end

#get_from_mappers(mappers) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/environmentor/attribute.rb', line 67

def get_from_mappers(mappers)
  return @value if defined?(@value)

  mappers.each do |mapper|
    begin
      @value = get_from_mapper(
        mapper, **mapper.class.opts_from_mappers_hash(@mappers_opts))
    rescue Environmentor::Mappers::ValueNotFound
      next
    else
      return @value
    end
  end

  return default unless Absent.equal?(default)
  if required?
    raise ValidationError::Missing.new(self, mappers, @mappers_opts)
  end
  nil
end

#validate_in_mappers(mappers) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/environmentor/attribute.rb', line 93

def validate_in_mappers(mappers)
  ValidationErrors.new.tap do |out|
    if required?
      begin
        get_from_mappers(mappers)
      rescue ValidationError => e
        out << e
      rescue StandardError => e
        out << ValidationError.new(self, mappers, @mappers_opts,
                                   message: e.message)
      end
    end
  end
end