Class: Pod::Specification::Consumer

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-core/specification/consumer.rb

Overview

Allows to conveniently access a Specification programmatically.

It takes care of:

  • standardizing the attributes
  • handling multi-platform values
  • handle default values
  • handle inherited values

This class allows to store the values of the attributes in the Specification as specified in the DSL. The benefits is reduced reliance on meta programming to access the attributes and the possibility of serializing a specification back exactly as defined in a file.

Instance Attribute Summary collapse

Regular attributes collapse

File patterns collapse

Preparing Values collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec, platform) ⇒ Consumer


31
32
33
34
35
36
37
38
# File 'lib/cocoapods-core/specification/consumer.rb', line 31

def initialize(spec, platform)
  @spec = spec
  @platform_name = platform.is_a?(Symbol) ? platform : platform.name

  unless spec.supported_on_platform?(platform)
    raise StandardError, "#{self} is not compatible with #{platform}."
  end
end

Instance Attribute Details

#platform_nameSymbol (readonly)


25
26
27
# File 'lib/cocoapods-core/specification/consumer.rb', line 25

def platform_name
  @platform_name
end

#specSpecification (readonly)


20
21
22
# File 'lib/cocoapods-core/specification/consumer.rb', line 20

def spec
  @spec
end

Class Method Details

.spec_attr_accessor(name) ⇒ Object

Creates a method to access the contents of the attribute.


48
49
50
51
52
# File 'lib/cocoapods-core/specification/consumer.rb', line 48

def self.spec_attr_accessor(name)
  define_method(name) do
    value_for_attribute(name)
  end
end

Instance Method Details

#_prepare_prefix_header_contents(value) ⇒ String

Converts the prefix header to a string if specified as an array.


333
334
335
336
337
338
# File 'lib/cocoapods-core/specification/consumer.rb', line 333

def _prepare_prefix_header_contents(value)
  if value
    value = value.join("\n") if value.is_a?(Array)
    value.strip_heredoc.chomp
  end
end

#_prepare_resource_bundles(value) ⇒ Hash

Ensures that the file patterns of the resource bundles are contained in an array.


348
349
350
351
352
353
354
355
356
# File 'lib/cocoapods-core/specification/consumer.rb', line 348

def _prepare_resource_bundles(value)
  result = {}
  if value
    value.each do |key, patterns|
      result[key] = [*patterns].compact
    end
  end
  result
end

#compiler_flagsArray<String>


86
# File 'lib/cocoapods-core/specification/consumer.rb', line 86

spec_attr_accessor :compiler_flags

#dependenciesArray<Dependency>


177
178
179
180
181
182
# File 'lib/cocoapods-core/specification/consumer.rb', line 177

def dependencies
  value = value_for_attribute(:dependencies)
  value.map do |name, requirements|
    Dependency.new(name, requirements)
  end
end

#exclude_filesArray<String>


166
# File 'lib/cocoapods-core/specification/consumer.rb', line 166

spec_attr_accessor :exclude_files

#frameworksArray<String>


71
# File 'lib/cocoapods-core/specification/consumer.rb', line 71

spec_attr_accessor :frameworks

#header_dirString


118
# File 'lib/cocoapods-core/specification/consumer.rb', line 118

spec_attr_accessor :header_dir

#header_mappings_dirString


123
# File 'lib/cocoapods-core/specification/consumer.rb', line 123

spec_attr_accessor :header_mappings_dir

#librariesArray<String>


81
# File 'lib/cocoapods-core/specification/consumer.rb', line 81

spec_attr_accessor :libraries

#merge_values(attr, existing_value, new_value) ⇒ String, ...

Merges the values of an attribute, either because the attribute is multi platform or because it is inherited.


266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/cocoapods-core/specification/consumer.rb', line 266

def merge_values(attr, existing_value, new_value)
  return existing_value if new_value.nil?
  return new_value if existing_value.nil?

  if attr.types.include?(TrueClass)
    new_value.nil? ? existing_value : new_value
  elsif attr.container == Array
    r = [*existing_value] + [*new_value]
    r.compact
  elsif attr.container == Hash
    existing_value.merge(new_value) do |_, old, new|
      if new.is_a?(Array) || old.is_a?(Array)
        r = [*old] + [*new]
        r.compact
      else
        old + ' ' + new
      end
    end
  else
    new_value
  end
end

#module_nameString


114
# File 'lib/cocoapods-core/specification/consumer.rb', line 114

spec_attr_accessor :module_name

#nameString


60
# File 'lib/cocoapods-core/specification/consumer.rb', line 60

spec_attr_accessor :name

#pod_target_xcconfigHash{String => String}


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

def pod_target_xcconfig
  attr = Specification::DSL.attributes[:pod_target_xcconfig]
  merge_values(attr, value_for_attribute(:xcconfig), value_for_attribute(:pod_target_xcconfig))
end

#prefix_header_contentsString


106
# File 'lib/cocoapods-core/specification/consumer.rb', line 106

spec_attr_accessor :prefix_header_contents

#prefix_header_fileString


110
# File 'lib/cocoapods-core/specification/consumer.rb', line 110

spec_attr_accessor :prefix_header_file

#prepare_hook_name(attr) ⇒ String

Note:

The hook is called after the value has been wrapped in an array (if needed according to the container) but before validation.

Returns the name of the prepare hook for this attribute.


322
323
324
# File 'lib/cocoapods-core/specification/consumer.rb', line 322

def prepare_hook_name(attr)
  "_prepare_#{attr.name}"
end

#prepare_value(attr, value) ⇒ Object

Note:

Only array containers are wrapped. To automatically wrap values for attributes with hash containers a prepare hook should be used.

Wraps a value in an Array if needed and calls the prepare hook to allow further customization of a value before storing it in the instance variable.


300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/cocoapods-core/specification/consumer.rb', line 300

def prepare_value(attr, value)
  if attr.container ==  Array
    value = [*value].compact
  end

  hook_name = prepare_hook_name(attr)
  if self.respond_to?(hook_name, true)
    send(hook_name, value)
  else
    value
  end
end

#preserve_pathsArray<String>


171
# File 'lib/cocoapods-core/specification/consumer.rb', line 171

spec_attr_accessor :preserve_paths

#private_header_filesArray<String>


139
# File 'lib/cocoapods-core/specification/consumer.rb', line 139

spec_attr_accessor :private_header_files

#public_header_filesArray<String>


135
# File 'lib/cocoapods-core/specification/consumer.rb', line 135

spec_attr_accessor :public_header_files

#raw_value_for_attribute(the_spec, attr) ⇒ String, ...

Returns the value of a given attribute taking into account multi platform values.


236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/cocoapods-core/specification/consumer.rb', line 236

def raw_value_for_attribute(the_spec, attr)
  value = the_spec.attributes_hash[attr.name.to_s]
  value = prepare_value(attr, value)

  if attr.multi_platform?
    if platform_hash = the_spec.attributes_hash[platform_name.to_s]
      platform_value = platform_hash[attr.name.to_s]
      platform_value = prepare_value(attr, platform_value)
      value = merge_values(attr, value, platform_value)
    end
  end
  value
end

#requires_arcBool Also known as: requires_arc?


65
# File 'lib/cocoapods-core/specification/consumer.rb', line 65

spec_attr_accessor :requires_arc

#resource_bundlesHash{String=>String}


155
# File 'lib/cocoapods-core/specification/consumer.rb', line 155

spec_attr_accessor :resource_bundles

#resourcesArray<String>


161
# File 'lib/cocoapods-core/specification/consumer.rb', line 161

spec_attr_accessor :resources

#source_filesArray<String>


131
# File 'lib/cocoapods-core/specification/consumer.rb', line 131

spec_attr_accessor :source_files

#user_target_xcconfigHash{String => String}


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

def user_target_xcconfig
  attr = Specification::DSL.attributes[:user_target_xcconfig]
  merge_values(attr, value_for_attribute(:xcconfig), value_for_attribute(:user_target_xcconfig))
end

#value_for_attribute(attr_name) ⇒ String, ...

Returns the value for the attribute with the given name for the specification. It takes into account inheritance, multi-platform attributes and default values.


197
198
199
200
201
202
203
# File 'lib/cocoapods-core/specification/consumer.rb', line 197

def value_for_attribute(attr_name)
  attr = Specification::DSL.attributes[attr_name]
  value = value_with_inheritance(spec, attr)
  value = attr.default(platform_name) if value.nil?
  value = attr.container.new if value.nil? && attr.container
  value
end

#value_with_inheritance(the_spec, attr) ⇒ String, ...

Returns the value of a given attribute taking into account inheritance.


215
216
217
218
219
220
221
222
223
# File 'lib/cocoapods-core/specification/consumer.rb', line 215

def value_with_inheritance(the_spec, attr)
  value = raw_value_for_attribute(the_spec, attr)
  if the_spec.root? || !attr.inherited?
    return value
  end

  parent_value = value_with_inheritance(the_spec.parent, attr)
  merge_values(attr, parent_value, value)
end

#vendored_frameworksArray<String>


144
# File 'lib/cocoapods-core/specification/consumer.rb', line 144

spec_attr_accessor :vendored_frameworks

#vendored_librariesArray<String>


149
# File 'lib/cocoapods-core/specification/consumer.rb', line 149

spec_attr_accessor :vendored_libraries

#weak_frameworksArray<String>


76
# File 'lib/cocoapods-core/specification/consumer.rb', line 76

spec_attr_accessor :weak_frameworks