Class: Pod::Specification::DSL::Attribute

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Options collapse

Accessors support collapse

Values validation collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Attribute

Returns a new attribute initialized with the given options.

Attributes by default are:

  • inherited

  • multi-platform

Parameters:

  • name (Symbol)

    @see name

  • options (Hash{Symbol=>Object})

    The options for configuring the attribute (see Options group).

Raises:

  • If there are unrecognized options.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 29

def initialize(name, options)
  @name = name

  @multi_platform = options.delete(:multi_platform) { true      }
  @inherited      = options.delete(:inherited)      { false     }
  @root_only      = options.delete(:root_only)      { false     }
  @required       = options.delete(:required)       { false     }
  @singularize    = options.delete(:singularize)    { false     }
  @file_patterns  = options.delete(:file_patterns)  { false     }
  @container      = options.delete(:container)      { nil       }
  @keys           = options.delete(:keys)           { nil       }
  @default_value  = options.delete(:default_value)  { nil       }
  @ios_default    = options.delete(:ios_default)    { nil       }
  @osx_default    = options.delete(:osx_default)    { nil       }
  @types          = options.delete(:types)          { [String] }

  unless options.empty?
    raise StandardError, "Unrecognized options: #{options} for #{self}"
  end
end

Instance Attribute Details

#containerClass (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.

Returns:

  • (Class)

    if defined it can be #Array or #Hash. It is used as default initialization value and to automatically wrap other values to arrays.



84
85
86
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 84

def container
  @container
end

#default_valueObject (readonly)

Note:

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.

Returns:

  • (Object)

    if the attribute follows configuration over convention it can specify a default value.



100
101
102
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 100

def default_value
  @default_value
end

#ios_defaultObject (readonly)

Returns similar to ##default_value but for iOS.

Returns:



104
105
106
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 104

def ios_default
  @ios_default
end

#keysArray, Hash (readonly)

Note:

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.

Returns:

  • (Array, Hash)

    the list of the accepted keys for an attribute wrapped by a Hash.



92
93
94
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 92

def keys
  @keys
end

#nameSymbol (readonly)

Returns the name of the attribute.

Returns:

  • (Symbol)

    the name of the attribute.



12
13
14
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 12

def name
  @name
end

#osx_defaultObject (readonly)

Returns similar to ##default_value but for OS X.

Returns:



108
109
110
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 108

def osx_default
  @osx_default
end

#typesArray<Class> (readonly)

Returns the list of the classes of the values supported by the attribute writer. If not specified defaults to #String.

Returns:

  • (Array<Class>)

    the list of the classes of the values supported by the attribute writer. If not specified defaults to #String.



71
72
73
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 71

def types
  @types
end

Instance Method Details

#allowed_keysArray

hash of a given specification.

Returns:

  • (Array)

    the flattened list of the allowed keys for the



236
237
238
239
240
241
242
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 236

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.

Parameters:

  • platform (Symbol) (defaults to: nil)

    the platform for which the default value is requested.

Returns:

  • (Object)

    The default value.



166
167
168
169
170
171
172
173
174
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 166

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

Note:

This is mostly used by the linter.

Returns whether the attribute describes file patterns.

Returns:

  • (Bool)

    whether the attribute describes file patterns.



142
143
144
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 142

def file_patterns?
  @file_patterns
end

#inherited?Bool

Note:

Attributes stored in wrappers are always inherited.

values with the parent.

Returns:

  • (Bool)

    defines whether the attribute reader should join the



151
152
153
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 151

def inherited?
  !root_only? && @inherited
end

#inspectString

Returns A string representation suitable for debugging.

Returns:

  • (String)

    A string representation suitable for debugging.



58
59
60
61
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 58

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.

Returns:

  • (Bool)

    whether the attribute is multi-platform and should work in conjunction with #PlatformProxy.



127
128
129
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 127

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.

Returns:

  • (Bool)

    whether the specification should be considered invalid if a value for the attribute is not specified.



113
114
115
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 113

def required?
  @required
end

#root_only?Bool

Returns whether the attribute should be specified only on the root specification.

Returns:

  • (Bool)

    whether the attribute should be specified only on the root specification.



120
121
122
# 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.

Returns:

  • (Bool)

    whether there should be a singular alias for the attribute writer.



134
135
136
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 134

def singularize?
  @singularize
end

#supported_typesArray<Class>

Returns the list of the classes of the values supported by the attribute, including the container.

Returns:

  • (Array<Class>)

    the list of the classes of the values supported by the attribute, including the container.



76
77
78
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 76

def supported_types
  @supported_types ||= @types.dup.push(container).compact
end

#to_sString

Returns A string representation suitable for UI.

Returns:

  • (String)

    A string representation suitable for UI.



52
53
54
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 52

def to_s
  "Specification attribute `#{name}`"
end

#validate_for_writing(spec, value) ⇒ void

This method returns an undefined value.

Validates a value before storing.

Raises:

  • If a root only attribute is set in a subspec.

  • If a unknown key is added to a hash.



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
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 218

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

Note:

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.

Raises:

  • If the type is not in the allowed list.



202
203
204
205
206
207
208
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 202

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_nameString

Returns the name of the setter method for the attribute.

Returns:

  • (String)

    the name of the setter method for the attribute.



178
179
180
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 178

def writer_name
  "#{name}="
end

#writer_singular_formString

Returns an aliased attribute writer offered for convenience on the DSL.

Returns:

  • (String)

    an aliased attribute writer offered for convenience on the DSL.



185
186
187
# File 'lib/cocoapods-core/specification/dsl/attribute.rb', line 185

def writer_singular_form
  "#{name.to_s.singularize}=" if singularize?
end