Class: CFBundle::Bundle

Inherits:
Object
  • Object
show all
Defined in:
lib/cfbundle/bundle.rb

Overview

A Bundle is an abstraction of a bundle accessible by the program.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Bundle

Opens the bundle and returns a new Bundle object.

A new #storage is automatically created from the file parameter:

  • If the file is a path to a bundle directory, a new Storage::FileSystem is created that references the bundle at the path.

  • If the file is an IO or a path to a ZIP archive, a new Storage::Zip is created that references the bundle within the archive. The rubyzip gem must be loaded and the archive is expected to contain a single bundle at its root (or inside a Payload directory for .ipa archives).

  • If the file is a Storage::Base, it is used as the bundle’s storage.

You should send #close when the bundle is no longer needed.

Note that storages created from an exisiting IO do not automatically close the file when the bundle is closed.

Parameters:

  • file (Object)

    The file to open.

Raises:

  • (ArgumentError)

    If the file cannot be opened.



53
54
55
# File 'lib/cfbundle/bundle.rb', line 53

def initialize(file)
  @storage = StorageDetection.open(file)
end

Instance Attribute Details

#storageStorage::Base (readonly)

The abstract storage used by the bundle.

The storage implements the methods that are used by Bundle to read the bundle from the underlying storage (ZIP archive or file system).



71
72
73
# File 'lib/cfbundle/bundle.rb', line 71

def storage
  @storage
end

Class Method Details

.open(file) {|bundle| ... } ⇒ Object, Bundle

Opens a bundle.

With no associated block, open is a synonym for new. If the optional code block is given, it will be passed the opened bundle as an argument and the Bundle object will automatically be closed when the block terminates. The value of the block will be returned from the method.

Parameters:

  • file (Object)

    The file to open. See #initialize for a description of the supported values.

Yield Parameters:

  • bundle (Bundle)

    The opened bundle. It is automatically closed when the block terminates.

Returns:

  • (Object)

    The return value of the block when a block if given.

  • (Bundle)

    The opened bundle when no block is given.



24
25
26
27
28
29
30
31
32
# File 'lib/cfbundle/bundle.rb', line 24

def self.open(file)
  bundle = new(file)
  return bundle unless block_given?
  begin
    yield bundle
  ensure
    bundle.close
  end
end

Instance Method Details

#build_versionString?

Returns the bundle’s build version number.

Returns:

  • (String, nil)

See Also:



89
90
91
# File 'lib/cfbundle/bundle.rb', line 89

def build_version
  info_string(CFBundle::INFO_KEY_BUNDLE_VERSION)
end

#closevoid

This method returns an undefined value.

Closes the bundle and its underlying storage.



59
60
61
# File 'lib/cfbundle/bundle.rb', line 59

def close
  @storage.close
end

#development_localizationString?

Returns the name of the development language of the bundle.

Returns:

  • (String, nil)

See Also:



129
130
131
# File 'lib/cfbundle/bundle.rb', line 129

def development_localization
  info_string(CFBundle::INFO_KEY_BUNDLE_DEVELOPMENT_REGION)
end

#display_nameString?

Returns the user-visible name of the bundle.

Returns:

  • (String, nil)

See Also:



122
123
124
# File 'lib/cfbundle/bundle.rb', line 122

def display_name
  info_string(CFBundle::INFO_KEY_BUNDLE_DISPLAY_NAME)
end

#executable_nameString?

Returns the name of the bundle’s executable file.

Returns:

  • (String, nil)

See Also:



136
137
138
# File 'lib/cfbundle/bundle.rb', line 136

def executable_name
  info_string(CFBundle::INFO_KEY_BUNDLE_EXECUTABLE)
end

#executable_pathString?

Returns the path of the bundle’s executable file.

The executable’s path is relative to the bundle’s path.

Returns:

  • (String, nil)

See Also:



145
146
147
148
# File 'lib/cfbundle/bundle.rb', line 145

def executable_path
  @executable_path ||=
    executable_name && lookup_executable_path(executable_name)
end

#find_resource(name, extension: nil, subdirectory: nil, localization: nil, preferred_languages: [], product: nil) ⇒ Resource?

Returns the first Resource object that matches the specified parameters.

Parameters:

  • name (String?, Rexgep?)

    The name to match or nil to match any name.

  • extension (String?) (defaults to: nil)

    The extension to match or nil to match any extension.

  • subdirectory (String?) (defaults to: nil)

    The name of the bundle subdirectory search.

  • localization (String?, Symbol?) (defaults to: nil)

    A language identifier to restrict the search to a specific localization.

  • preferred_languages (Array) (defaults to: [])

    An array of strings (or symbols) corresponding to a user’s preferred languages.

  • product (String?) (defaults to: nil)

    The product to match or nil to match any product.

Returns:

See Also:



196
197
198
199
200
201
202
203
204
# File 'lib/cfbundle/bundle.rb', line 196

def find_resource(name, extension: nil, subdirectory: nil,
                  localization: nil, preferred_languages: [], product: nil)
  Resource.foreach(
    self, name,
    extension: extension, subdirectory: subdirectory,
    localization: localization, preferred_languages: preferred_languages,
    product: product
  ).first
end

#find_resources(name, extension: nil, subdirectory: nil, localization: nil, preferred_languages: [], product: nil) ⇒ Array

Returns all the Resource objects that matches the specified parameters.

Parameters:

  • name (String?, Rexgep?)

    The name to match or nil to match any name.

  • extension (String?) (defaults to: nil)

    The extension to match or nil to match any extension.

  • subdirectory (String?) (defaults to: nil)

    The name of the bundle subdirectory to search.

  • localization (String?, Symbol?) (defaults to: nil)

    A language identifier to restrict the search to a specific localization.

  • preferred_languages (Array) (defaults to: [])

    An array of strings (or symbols) corresponding to a user’s preferred languages.

  • product (String?) (defaults to: nil)

    The product to match or nil to match any product.

Returns:

  • (Array)

    An array of Resource objects.

See Also:



222
223
224
225
226
227
228
229
230
# File 'lib/cfbundle/bundle.rb', line 222

def find_resources(name, extension: nil, subdirectory: nil,
                   localization: nil, preferred_languages: [], product: nil)
  Resource.foreach(
    self, name,
    extension: extension, subdirectory: subdirectory,
    localization: localization, preferred_languages: preferred_languages,
    product: product
  ).to_a
end

#identifierString?

Returns the bundle identifier from the bundle’s information property list.

Returns:

  • (String, nil)

See Also:



82
83
84
# File 'lib/cfbundle/bundle.rb', line 82

def identifier
  info_string(CFBundle::INFO_KEY_BUNDLE_IDENTIFIER)
end

#infoHash

Returns the bundle’s information property list hash.

Returns:

  • (Hash)


75
76
77
# File 'lib/cfbundle/bundle.rb', line 75

def info
  @info ||= Plist.load_info_plist(self)
end

#localizationsArray

Returns a list of all the localizations contained in the bundle.

Returns:

  • (Array)


167
168
169
# File 'lib/cfbundle/bundle.rb', line 167

def localizations
  @localizations ||= Localization.localizations_in(self)
end

#nameString?

Returns the short name of the bundle.

Returns:

  • (String, nil)

See Also:



115
116
117
# File 'lib/cfbundle/bundle.rb', line 115

def name
  info_string(CFBundle::INFO_KEY_BUNDLE_NAME)
end

#package_typeString?

Returns the bundle’s OS Type code.

The value for this key consists of a four-letter code.



108
109
110
# File 'lib/cfbundle/bundle.rb', line 108

def package_type
  info_string(CFBundle::INFO_KEY_BUNDLE_PACKAGE_TYPE)
end

#preferred_localizations(preferred_languages) ⇒ Array

Returns an ordered list of preferred localizations contained in the bundle.

Parameters:

  • preferred_languages (Array)

    An array of strings (or symbols) corresponding to a user’s preferred languages.

Returns:

  • (Array)


176
177
178
# File 'lib/cfbundle/bundle.rb', line 176

def preferred_localizations(preferred_languages)
  Localization.preferred_localizations(localizations, preferred_languages)
end

#release_versionString?

Returns the bundle’s release version number.

Returns:

  • (String, nil)

See Also:



96
97
98
# File 'lib/cfbundle/bundle.rb', line 96

def release_version
  info_string(CFBundle::INFO_KEY_BUNDLE_SHORT_VERSION_STRING)
end

#resources_directoryString

Returns the path of the bundle’s subdirectory that contains its resources.

The path is relative to the bundle’s path. For iOS application bundles, as the resources directory is the bundle, this method returns a single dot (.).

Returns:

  • (String)

See Also:



157
158
159
160
161
162
163
# File 'lib/cfbundle/bundle.rb', line 157

def resources_directory
  case layout_version
  when 0 then 'Resources'
  when 2 then 'Contents/Resources'
  when 3 then '.'
  end
end