Class: Kamaze::Project::Tools::Gemspec::Writer::Dependency
- 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
-
#spec_name ⇒ Object
readonly
protected
Returns the value of attribute spec_name.
Instance Method Summary collapse
-
#initialize(dependencies, spec_name = 's') ⇒ Dependency
constructor
A new instance of Dependency.
- #keep(*keep) ⇒ self
- #make_spec_line(gem, type) ⇒ String protected
-
#to_a ⇒ Array<Gem::Dependency>
Get dependencies array representation.
-
#to_h ⇒ Hash<Symbol, Gem::Dependency>
Get dependencies hash representation.
-
#to_s ⇒ String
Get dependencies string representation.
Constructor Details
#initialize(dependencies, spec_name = 's') ⇒ Dependency
Returns a new instance of Dependency.
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_name ⇒ Object (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
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)
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_a ⇒ Array<Gem::Dependency>
Get dependencies array representation.
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_h ⇒ Hash<Symbol, Gem::Dependency>
Get dependencies hash representation.
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_s ⇒ String
Get dependencies string representation.
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 |