Class: Environmentor::Attribute
- Inherits:
-
Object
- Object
- Environmentor::Attribute
- Defined in:
- lib/environmentor/attribute.rb
Defined Under Namespace
Classes: ValidationError, ValidationErrors
Constant Summary collapse
- Absent =
Object.new
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#namespace_chain ⇒ Object
readonly
Returns the value of attribute namespace_chain.
-
#required ⇒ Object
(also: #required?)
readonly
Returns the value of attribute required.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#type_coercer ⇒ Object
writeonly
Sets the attribute type_coercer.
Instance Method Summary collapse
- #clear_cache! ⇒ Object
- #full_name ⇒ Object
- #get_from_mapper(mapper, **opts) ⇒ Object
- #get_from_mappers(mappers) ⇒ Object
-
#initialize(name, namespace_chain, type: :string, required: true, default: Absent, description: nil, help: nil, mappers: {}) ⇒ Attribute
constructor
A new instance of Attribute.
- #validate_in_mappers(mappers) ⇒ Object
Constructor Details
#initialize(name, namespace_chain, type: :string, required: true, default: Absent, description: nil, help: nil, mappers: {}) ⇒ Attribute
Returns a new instance of Attribute.
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
#default ⇒ Object (readonly)
Returns the value of attribute default.
46 47 48 |
# File 'lib/environmentor/attribute.rb', line 46 def default @default end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
46 47 48 |
# File 'lib/environmentor/attribute.rb', line 46 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
46 47 48 |
# File 'lib/environmentor/attribute.rb', line 46 def name @name end |
#namespace_chain ⇒ Object (readonly)
Returns the value of attribute namespace_chain.
46 47 48 |
# File 'lib/environmentor/attribute.rb', line 46 def namespace_chain @namespace_chain end |
#required ⇒ Object (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 |
#type ⇒ Object (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
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_name ⇒ Object
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.) end end end end |