Class: Jewel::Gem::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/jewel/gem/metadata.rb

Overview

Stores Ruby gem metadata.

Author:

  • Matheus Afonso Martins Moreira

Since:

  • 0.0.1

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Metadata

Creates a new, empty set of gem metadata.

If given a block, it will be evaluated in the context of the new instance.

Parameters:

  • block (Proc)

    the initializer block

Since:

  • 0.0.1



16
17
18
# File 'lib/jewel/gem/metadata.rb', line 16

def initialize(&block)
  instance_eval &block unless block.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object

Assigns or returns attributes from the associated gem specification. If it doesn’t respond to it, the attribute will be stored in this object.

Since:

  • 0.0.1



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jewel/gem/metadata.rb', line 23

def method_missing(method_name, *arguments, &block)
  method = method_name.to_s.gsub(/[=?!]+\z/ix, '').strip.intern
  count = arguments.count
  if count.zero?
    if spec.respond_to? method
      spec.send method
    else
      stored_attributes[method]
    end
  else
    writer_method = '='.prepend(method.to_s).intern
    if spec.respond_to? writer_method
      spec.send writer_method, *arguments
    else
      value = count == 1 ? arguments.first : arguments
      stored_attributes[method] = value
    end
  end
end

Instance Method Details

#all_dependenciesHash<String, Array<String>>

Runtime and development dependencies. The former takes precedence in case of conflict.

Returns:

  • (Hash<String, Array<String>>)

    names of gems associated with their requirements

Since:

  • 0.0.2



67
68
69
# File 'lib/jewel/gem/metadata.rb', line 67

def all_dependencies
  development_dependencies.merge dependencies
end

#dependenciesHash<String, Array<String>> Also known as: runtime_dependencies

This gem’s runtime dependencies.

Returns:

  • (Hash<String, Array<String>>)

    names of gems associated with their requirements

Since:

  • 0.0.1



47
48
49
# File 'lib/jewel/gem/metadata.rb', line 47

def dependencies
  convert_to_hash specification.runtime_dependencies
end

#development_dependenciesHash<String, Array<String>>

This gem’s development dependencies.

Returns:

  • (Hash<String, Array<String>>)

    names of gems associated with their requirements

Since:

  • 0.0.1



57
58
59
# File 'lib/jewel/gem/metadata.rb', line 57

def development_dependencies
  convert_to_hash specification.development_dependencies
end

#each_dependency(options = {}) {|gem_name, *requirements| ... } ⇒ Object

Yields dependencies to the given block, or returns an enumerator.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :development (true, false, :only) — default: false

    whether runtime and development dependencies should be included

Yields:

  • (gem_name, *requirements)

Yield Parameters:

  • gem_name (String)

    the name of the gem

  • requirements (Array<String>)

    the version requirements

Since:

  • 0.0.2



79
80
81
82
83
84
85
86
87
# File 'lib/jewel/gem/metadata.rb', line 79

def each_dependency(options = {}, &block)
  return enum_for :each_dependency, options unless block_given?
  development = options.fetch :development, false
  case development
    when :only then development_dependencies
    when  true then all_dependencies
    else dependencies
  end.each &block
end

#gem_specification(existing_specification = nil) ⇒ ::Gem::Specification Also known as: spec, gemspec, to_spec, specification

The Gem::Specification. If passed an existing instance, it will be used instead.

Parameters:

  • existing_specification (::Gem::Specification) (defaults to: nil)

    the existing instance

Returns:

  • (::Gem::Specification)

    the specification

Since:

  • 0.0.4



96
97
98
99
100
101
# File 'lib/jewel/gem/metadata.rb', line 96

def gem_specification(existing_specification = nil)
  unless existing_specification.nil?
    @gem_specification = existing_specification
  end
  @gem_specification ||= ::Gem::Specification.new
end