Class: Kamaze::Project::Tools::Gemspec::Writer::Dependency

Inherits:
Object
  • Object
show all
Defined in:
lib/kamaze/project/tools/gemspec/writer/dependency.rb

Overview

Describe dependency as an Hash indexed by type

Sample of use:

selector = lambda do |type|
  Bundler.environment.dependencies
         .select { |d| d.groups.include?(type) }.to_a
end

dependency = Dependency.new({
  runtime: selector.call(:default),
  development: selector.call(:development),
})

puts dependency.keep(:runtime).to_s

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dependencies, spec_name = 's') ⇒ Dependency

Returns a new instance of Dependency.

Parameters:

  • dependencies (Hash<Symbol, Gem::Dependency>)
  • spec_name (String) (defaults to: 's')


31
32
33
34
35
# File 'lib/kamaze/project/tools/gemspec/writer/dependency.rb', line 31

def initialize(dependencies, spec_name = 's')
  @dependencies = dependencies.to_h.freeze
  @spec_name = spec_name.to_s
  @keep = [:runtime, :development]
end

Instance Attribute Details

#spec_nameObject (readonly, protected)

Returns the value of attribute spec_name.



90
91
92
# File 'lib/kamaze/project/tools/gemspec/writer/dependency.rb', line 90

def spec_name
  @spec_name
end

Instance Method Details

#keep(*keep) ⇒ self

Returns:

  • (self)


38
39
40
41
42
# File 'lib/kamaze/project/tools/gemspec/writer/dependency.rb', line 38

def keep(*keep)
  @keep = keep

  self
end

#make_spec_line(gem, type) ⇒ String (protected)

Parameters:

  • gem (Bundler::Dependency)
  • type (String|Symbol)

Returns:

  • (String)


95
96
97
98
99
100
101
102
103
# File 'lib/kamaze/project/tools/gemspec/writer/dependency.rb', line 95

def make_spec_line(gem, type)
  '%<spacer>s%<spec_name>s.%<method>s("%<gem>s", %<requirements>s)' % {
    spacer: "\s" * 2,
    spec_name: spec_name,
    method: "add_#{type}_dependency",
    gem: gem.name,
    requirements: gem.requirements_list
  }
end

#to_aArray<Gem::Dependency>

Get dependencies array representation.

Returns:

  • (Array<Gem::Dependency>)


76
77
78
79
80
81
82
83
84
85
86
# File 'lib/kamaze/project/tools/gemspec/writer/dependency.rb', line 76

def to_a
  out = []

  @dependencies.each do |type, gems|
    next unless @keep.include?(type)

    out.concat(gems)
  end

  out
end

#to_hHash<Symbol, Gem::Dependency>

Get dependencies hash representation.

Returns:

  • (Hash<Symbol, Gem::Dependency>)


47
48
49
50
51
52
53
54
55
56
# File 'lib/kamaze/project/tools/gemspec/writer/dependency.rb', line 47

def to_h
  out = {}
  @dependencies.each do |type, gems|
    next unless @keep.include?(type)

    out[type] = Array.new(gems.clone)
  end

  out
end

#to_sString

Get dependencies string representation.

Returns:

  • (String)


61
62
63
64
65
66
67
68
69
70
71
# File 'lib/kamaze/project/tools/gemspec/writer/dependency.rb', line 61

def to_s
  lines = []

  self.to_h.each do |type, gems|
    gems.each do |gem|
      lines << make_spec_line(gem, type)
    end
  end

  lines.join("\n").rstrip
end