Class: Epuber::DSL::Attribute
- Inherits:
-
Object
- Object
- Epuber::DSL::Attribute
- Defined in:
- lib/epuber/dsl/attribute.rb
Overview
Stores the information of an attribute. It also provides logic to implement any required logic.
Options collapse
-
#container ⇒ Class
readonly
If defined it can be [Array] or [Hash].
-
#default_value ⇒ Object
readonly
If the attribute follows configuration over convention it can specify a default value.
-
#file_patterns ⇒ Bool
(also: #file_patterns?)
readonly
Whether the attribute describes file patterns.
-
#keys ⇒ Array, Hash
readonly
The list of the accepted keys for an attribute wrapped by a Hash.
-
#required ⇒ Bool
(also: #required?)
readonly
Whether the specification should be considered invalid if a value for the attribute is not specified.
-
#root_only ⇒ Bool
(also: #root_only?)
readonly
Whether the attribute should be specified only on the root specification.
-
#singularize ⇒ Bool
(also: #singularize?)
readonly
Whether there should be a singular alias for the attribute writer.
-
#types ⇒ Array<Class>
readonly
The list of the classes of the values supported by the attribute writer.
Instance Attribute Summary collapse
-
#name ⇒ Symbol
readonly
Name of attribute.
Options collapse
-
#inherited? ⇒ Bool
Defines whether the attribute reader should join the values with the parent.
-
#supported_types ⇒ Array<Class>
The list of the classes of the values supported by the attribute, including the container.
-
#writer_name ⇒ String
The name of the setter method for the attribute.
-
#writer_singular_form ⇒ String
An aliased attribute writer offered for convenience on the DSL.
Values validation collapse
-
#validate_for_writing(spec, value) ⇒ void
Validates a value before storing.
-
#validate_type(value) ⇒ void
Validates the value for an attribute.
Automatic conversion collapse
-
#converted_value(value) ⇒ Object
Converts value to compatible type of attribute.
Instance Method Summary collapse
-
#initialize(name, inherited: false, root_only: false, required: false, singularize: false, file_patterns: false, container: nil, keys: nil, default_value: nil, auto_convert: {}, types: nil) ⇒ Attribute
constructor
Returns a new attribute initialized with the given options.
-
#inspect ⇒ String
A string representation suitable for debugging.
-
#to_s ⇒ String
A string representation suitable for UI.
Constructor Details
#initialize(name, inherited: false, root_only: false, required: false, singularize: false, file_patterns: false, container: nil, keys: nil, default_value: nil, auto_convert: {}, types: nil) ⇒ Attribute
Returns a new attribute initialized with the given options.
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 51 52 53 54 55 56 57 |
# File 'lib/epuber/dsl/attribute.rb', line 26 def initialize(name, inherited: false, root_only: false, required: false, singularize: false, file_patterns: false, container: nil, keys: nil, default_value: nil, auto_convert: {}, types: nil) @name = name @inherited = inherited @root_only = root_only @required = required @singularize = singularize @file_patterns = file_patterns @container = container @keys = keys @default_value = default_value @auto_convert = auto_convert @types = if !types.nil? types elsif @default_value && @auto_convert.empty? [@default_value.class] elsif !@auto_convert.empty? [@auto_convert.values.first] else [String] end end |
Instance Attribute Details
#container ⇒ Class (readonly)
Returns if defined it can be [Array] or [Hash]. It is used as default initialization value and to automatically wrap other values to arrays.
92 93 94 |
# File 'lib/epuber/dsl/attribute.rb', line 92 def container @container end |
#default_value ⇒ Object (readonly)
The default value is not automatically wrapped and should be specified within the container if any.
Returns if the attribute follows configuration over convention it can specify a default value.
106 107 108 |
# File 'lib/epuber/dsl/attribute.rb', line 106 def default_value @default_value end |
#file_patterns ⇒ Bool (readonly) Also known as: file_patterns?
This is mostly used by the linter.
Returns whether the attribute describes file patterns.
129 130 131 |
# File 'lib/epuber/dsl/attribute.rb', line 129 def file_patterns @file_patterns end |
#keys ⇒ Array, Hash (readonly)
A hash is accepted to group the keys associated only with certain keys (see the source attribute of a Book).
Returns the list of the accepted keys for an attribute wrapped by a Hash.
99 100 101 |
# File 'lib/epuber/dsl/attribute.rb', line 99 def keys @keys end |
#name ⇒ Symbol (readonly)
Returns name of attribute.
14 15 16 |
# File 'lib/epuber/dsl/attribute.rb', line 14 def name @name end |
#required ⇒ Bool (readonly) Also known as: required?
Returns whether the specification should be considered invalid if a value for the attribute is not specified.
111 112 113 |
# File 'lib/epuber/dsl/attribute.rb', line 111 def required @required end |
#root_only ⇒ Bool (readonly) Also known as: root_only?
Returns whether the attribute should be specified only on the root specification.
116 117 118 |
# File 'lib/epuber/dsl/attribute.rb', line 116 def root_only @root_only end |
#singularize ⇒ Bool (readonly) Also known as: singularize?
Returns whether there should be a singular alias for the attribute writer.
122 123 124 |
# File 'lib/epuber/dsl/attribute.rb', line 122 def singularize @singularize end |
#types ⇒ Array<Class> (readonly)
Returns the list of the classes of the values supported by the attribute writer. If not specified defaults to [String].
80 81 82 |
# File 'lib/epuber/dsl/attribute.rb', line 80 def types @types end |
Instance Method Details
#converted_value(value) ⇒ Object
Converts value to compatible type of attribute
Can be configured with option :auto_convert
Supports conversion from type to type, eg `{ String => Fixnum }`
also from types to type eg `{ [String, Date] => Fixnum }`
Supports custom conversion with Proc, eg `{ String => lambda { |value| value.to_s } }`
also with multiple types
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/epuber/dsl/attribute.rb', line 218 def converted_value(value) begin validate_type(value) rescue StandardError raise if @auto_convert.nil? dest_class = @auto_convert[value.class] if dest_class.nil? array_keys = @auto_convert.select { |k, _v| k.is_a?(Array) } array_keys_with_type = array_keys.select { |k, _v| k.any? { |klass| value.class <= klass } } dest_class = array_keys_with_type.values.first if array_keys_with_type.count.positive? end if dest_class.respond_to?(:call) return dest_class.call(value) elsif dest_class.respond_to?(:parse) return dest_class.parse(value) elsif dest_class <= String return value.to_s elsif dest_class.respond_to?(:new) return dest_class.new(value) else raise StandardError, "Object/class #{dest_class} doesn't support any convert method (#call, .parse or implicit .new)" end end value end |
#inherited? ⇒ Bool
Attributes stored in wrappers are always inherited.
Returns defines whether the attribute reader should join the values with the parent.
136 137 138 |
# File 'lib/epuber/dsl/attribute.rb', line 136 def inherited? !root_only? && @inherited end |
#inspect ⇒ String
Returns A string representation suitable for debugging.
69 70 71 |
# File 'lib/epuber/dsl/attribute.rb', line 69 def inspect "<#{self.class} name=#{name} types=#{types}>" end |
#supported_types ⇒ Array<Class>
Returns the list of the classes of the values supported by the attribute, including the container.
85 86 87 |
# File 'lib/epuber/dsl/attribute.rb', line 85 def supported_types @supported_types ||= @types.dup.push(container).compact end |
#to_s ⇒ String
Returns A string representation suitable for UI.
63 64 65 |
# File 'lib/epuber/dsl/attribute.rb', line 63 def to_s "Attribute `#{name}`" end |
#validate_for_writing(spec, value) ⇒ void
This method returns an undefined value.
Validates a value before storing.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/epuber/dsl/attribute.rb', line 182 def validate_for_writing(spec, value) if root_only? && !spec.root? raise StandardError, "Can't set `#{name}` attribute for subspecs (in `#{spec.name}`)." end return unless keys # @return [Array] the flattened list of the allowed keys for the hash of a given specification. # allowed_keys = lambda do if keys.is_a?(Hash) keys.keys.concat(keys.values.flatten.compact) else keys end end value.each_key do |key| unless allowed_keys.include?(key) raise StandardError, "Unknown key `#{key}` for #{self}. Allowed keys: `#{allowed_keys.inspect}`" end end end |
#validate_type(value) ⇒ void
The this is called before preparing the value.
This method returns an undefined value.
Validates the value for an attribute. This validation should be performed before the value is prepared or wrapped.
167 168 169 170 171 172 |
# File 'lib/epuber/dsl/attribute.rb', line 167 def validate_type(value) return if value.nil? return if supported_types.any? { |klass| value.class <= klass } raise StandardError, "Non acceptable type `#{value.class}` for #{self}. Allowed types: `#{types.inspect}`" end |
#writer_name ⇒ String
Returns the name of the setter method for the attribute.
144 145 146 |
# File 'lib/epuber/dsl/attribute.rb', line 144 def writer_name "#{name}=" end |
#writer_singular_form ⇒ String
Returns an aliased attribute writer offered for convenience on the DSL.
150 151 152 |
# File 'lib/epuber/dsl/attribute.rb', line 150 def writer_singular_form "#{name.to_s.singularize}=" if singularize? end |