Class: OrganizeGemfile::GemfileGroup

Inherits:
Struct
  • Object
show all
Defined in:
lib/organize_gemfile/gemfile_group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, specs = []) ⇒ GemfileGroup

Returns a new instance of GemfileGroup.

[View source]

3
4
5
# File 'lib/organize_gemfile/gemfile_group.rb', line 3

def initialize(name, specs = [])
  super
end

Instance Attribute Details

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name


2
3
4
# File 'lib/organize_gemfile/gemfile_group.rb', line 2

def name
  @name
end

#specsObject

Returns the value of attribute specs

Returns:

  • (Object)

    the current value of specs


2
3
4
# File 'lib/organize_gemfile/gemfile_group.rb', line 2

def specs
  @specs
end

Instance Method Details

#to_gemfile_linesObject

[View source]

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/organize_gemfile/gemfile_group.rb', line 7

def to_gemfile_lines
  lines = []
  indent_spaces = ""
  max_length = 0

  is_before_default = name == :before_default
  is_default = name == [:default]

  if !is_before_default && !is_default
    indent_spaces = "  "
    lines << "group #{name.map { |n| ":#{n}" }.join(", ")} do"
  end

  spec_lines = specs.sort_by { |s| s[:name] }.map do |spec|
    parts = []
    parts << "gem \"#{spec[:name]}\""
    if spec[:version] && spec[:version] != ">= 0"
      version_parts = []
      spec[:version].split(",").each do |version|
        version_parts << "\"#{version.strip}\""
      end
      parts << version_parts.join(", ")
    end
    parts << "require: \"#{spec[:requires].first}\"" if spec[:requires].first && spec[:requires].first != spec[:name]
    parts << "require: false" if spec[:requires] == []
    parts << "platforms: %i[ #{spec[:platforms].join(" ")} ]" if spec[:platforms].any?
    line = parts.join(", ")

    if spec[:summary]
      max_length = [max_length, line.length].max
    end

    {line: line, spec: spec}
  end

  spec_lines.each do |l|
    line = l[:line]
    spec = l[:spec]

    if spec[:summary]
      line = line.ljust(max_length)
      line += " # #{spec[:summary]}"
    end
    lines << indent_spaces + line
  end

  if !is_before_default && !is_default
    lines << "end"
  end

  lines
end