Class: CFBundle::Resource

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

Overview

A Resource is an abstraction of file contained within a Bundle.

Defined Under Namespace

Classes: Enumerator, Predicate

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bundle, path) ⇒ Resource

Returns a new instance of Resource.

Parameters:

  • bundle (Bundle)

    The resource’s enclosing bundle.

  • path (String)

    The path of the resource within the bundle.



16
17
18
19
# File 'lib/cfbundle/resource.rb', line 16

def initialize(bundle, path)
  @bundle = bundle
  @path = path
end

Instance Attribute Details

#bundleBundle (readonly)

Returns the resource’s enclosing bundle.

Returns:



8
9
10
# File 'lib/cfbundle/resource.rb', line 8

def bundle
  @bundle
end

#pathString (readonly)

Returns the path to the resource within the bundle.

Returns:

  • (String)


12
13
14
# File 'lib/cfbundle/resource.rb', line 12

def path
  @path
end

Class Method Details

.foreach(bundle, name, extension: nil, subdirectory: nil, localization: nil, preferred_languages: [], product: nil) {|resource| ... } ⇒ nil, Enumerator

Enumerates the resources in a bundle that match the specified parameters.

Parameters:

  • bundle (Bundle)

    The bundle that contains the resources.

  • 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.

  • 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.

Yield Parameters:

Returns:

  • (nil)

    When a block is given.

  • (Enumerator)

    When no block is given.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cfbundle/resource.rb', line 50

def self.foreach(bundle, name, extension: nil, subdirectory: nil,
                 localization: nil, preferred_languages: [], product: nil,
                 &block)
  enumerator = ::Enumerator.new do |y|
    enumerator = Enumerator.new(bundle, subdirectory, localization,
                                preferred_languages)
    predicate = Predicate.new(name, extension, product)
    loop do
      resource = enumerator.next
      y << resource if predicate.match?(resource)
    end
  end
  enumerator.each(&block)
end

Instance Method Details

#open {|file| ... } ⇒ Object, IO

Opens the resource for reading.

With no associated block, the method returns an IO. If the optional block is given, it will be passed the opened file and the file will automatically be closed when the block terminates.

Yield Parameters:

  • file (IO)

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

Returns:

  • (Object)

    The return value to the block when a block is given.

  • (IO)

    The opened file. When no block is given.



30
31
32
# File 'lib/cfbundle/resource.rb', line 30

def open(&block)
  bundle.storage.open(path, &block)
end