Class: Pod::Specification::DSL::Attribute
- Inherits:
-
Object
- Object
- Pod::Specification::DSL::Attribute
- Defined in:
- lib/cocoapods-core/specification/dsl/attribute.rb
Overview
A Specification attribute 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.
-
#ios_default ⇒ Object
readonly
Similar to ##default_value but for iOS.
-
#keys ⇒ Array, Hash
readonly
The list of the accepted keys for an attribute wrapped by a Hash.
-
#osx_default ⇒ Object
readonly
Similar to ##default_value but for OS X.
-
#types ⇒ Array<Class>
readonly
The list of the classes of the values supported by the attribute writer.
Instance Attribute Summary collapse
-
#name ⇒ Symbol
readonly
The name of the attribute.
Options collapse
-
#file_patterns? ⇒ Bool
Whether the attribute describes file patterns.
-
#inherited? ⇒ Bool
values with the parent.
-
#multi_platform? ⇒ Bool
Whether the attribute is multi-platform and should work in conjunction with #PlatformProxy.
-
#required? ⇒ Bool
Whether the specification should be considered invalid if a value for the attribute is not specified.
-
#root_only? ⇒ Bool
Whether the attribute should be specified only on the root specification.
-
#singularize? ⇒ Bool
Whether there should be a singular alias for the attribute writer.
-
#supported_types ⇒ Array<Class>
The list of the classes of the values supported by the attribute, including the container.
Accessors support collapse
-
#default(platform = nil) ⇒ Object
Returns the default value for the attribute.
-
#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
-
#allowed_keys ⇒ Array
hash of a given specification.
-
#validate_for_writing(spec, value) ⇒ void
Validates a value before storing.
-
#validate_type(value) ⇒ void
Validates the value for an attribute.
Instance Method Summary collapse
-
#initialize(name, options) ⇒ 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, options) ⇒ Attribute
Returns a new attribute initialized with the given options.
Attributes by default are:
-
inherited
-
multi-platform
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 31 def initialize(name, ) @name = name @multi_platform = .delete(:multi_platform) { true } @inherited = .delete(:inherited) { false } @root_only = .delete(:root_only) { false } @required = .delete(:required) { false } @singularize = .delete(:singularize) { false } @file_patterns = .delete(:file_patterns) { false } @container = .delete(:container) { nil } @keys = .delete(:keys) { nil } @default_value = .delete(:default_value) { nil } @ios_default = .delete(:ios_default) { nil } @osx_default = .delete(:osx_default) { nil } @types = .delete(:types) { [String] } unless .empty? raise StandardError, "Unrecognized options: #{} for #{self}" 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.
86 87 88 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 86 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.
102 103 104 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 102 def default_value @default_value end |
#ios_default ⇒ Object (readonly)
Returns similar to ##default_value but for iOS.
106 107 108 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 106 def ios_default @ios_default end |
#keys ⇒ Array, Hash (readonly)
A hash is accepted to group the keys associated only with certain keys (see the source attribute of a Spec).
Returns the list of the accepted keys for an attribute wrapped by a Hash.
94 95 96 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 94 def keys @keys end |
#name ⇒ Symbol (readonly)
Returns the name of the attribute.
14 15 16 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 14 def name @name end |
#osx_default ⇒ Object (readonly)
Returns similar to ##default_value but for OS X.
110 111 112 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 110 def osx_default @osx_default 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.
73 74 75 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 73 def types @types end |
Instance Method Details
#allowed_keys ⇒ Array
hash of a given specification.
228 229 230 231 232 233 234 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 228 def allowed_keys if keys.is_a?(Hash) keys.keys.concat(keys.values.flatten.compact) else keys end end |
#default(platform = nil) ⇒ Object
Returns the default value for the attribute.
158 159 160 161 162 163 164 165 166 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 158 def default(platform = nil) if platform && multi_platform? platform_value = ios_default if platform == :ios platform_value = osx_default if platform == :osx platform_value || default_value else default_value end end |
#file_patterns? ⇒ Bool
This is mostly used by the linter.
Returns whether the attribute describes file patterns.
136 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 136 def file_patterns?; @file_patterns; end |
#inherited? ⇒ Bool
Attributes stored in wrappers are always inherited.
values with the parent.
143 144 145 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 143 def inherited? !root_only? && @inherited end |
#inspect ⇒ String
Returns A string representation suitable for debugging.
60 61 62 63 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 60 def inspect "<#{self.class} name=#{name} types=#{types} " \ "multi_platform=#{multi_platform?}>" end |
#multi_platform? ⇒ Bool
Returns whether the attribute is multi-platform and should work in conjunction with #PlatformProxy.
125 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 125 def multi_platform?; @multi_platform; end |
#required? ⇒ Bool
Returns whether the specification should be considered invalid if a value for the attribute is not specified.
115 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 115 def required?; @required; end |
#root_only? ⇒ Bool
Returns whether the attribute should be specified only on the root specification.
120 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 120 def root_only?; @root_only; end |
#singularize? ⇒ Bool
Returns whether there should be a singular alias for the attribute writer.
130 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 130 def singularize?; @singularize; end |
#supported_types ⇒ Array<Class>
Returns the list of the classes of the values supported by the attribute, including the container.
78 79 80 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 78 def supported_types @supported_types ||= @types.dup.push(container).compact end |
#to_s ⇒ String
Returns A string representation suitable for UI.
54 55 56 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 54 def to_s "Specification attribute `#{name}`" end |
#validate_for_writing(spec, value) ⇒ void
This method returns an undefined value.
Validates a value before storing.
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 210 def validate_for_writing(spec, value) if root_only? && !spec.root? raise StandardError, "Can't set `#{name}` attribute for " \ "subspecs (in `#{spec.name}`)." end if keys value.keys.each do |key| unless allowed_keys.include?(key) raise StandardError, "Unknown key `#{key}` for "\ "#{self}. Allowed keys: `#{allowed_keys.inspect}`" end end end # @return [Array] the flattened list of the allowed keys for the # hash of a given specification. # def allowed_keys if keys.is_a?(Hash) keys.keys.concat(keys.values.flatten.compact) else keys 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.
194 195 196 197 198 199 200 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 194 def validate_type(value) return if value.nil? unless supported_types.any? { |klass| value.class == klass } raise StandardError, "Non acceptable type `#{value.class}` for "\ "#{self}. Allowed values: `#{types.inspect}`" end end |
#writer_name ⇒ String
Returns the name of the setter method for the attribute.
170 171 172 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 170 def writer_name "#{name}=" end |
#writer_singular_form ⇒ String
Returns an aliased attribute writer offered for convenience on the DSL.
177 178 179 |
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 177 def writer_singular_form "#{name.to_s.singularize}=" if singularize? end |