Class: GemfileParser::Commands::Groups

Inherits:
Object
  • Object
show all
Defined in:
lib/gemfile_parser/commands/groups.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gem_name:, gemfile_path:, ignore:, delimiter:) ⇒ Groups

Returns a new instance of Groups.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gemfile_parser/commands/groups.rb', line 28

def initialize(
  gem_name:,
  gemfile_path:,
  ignore:,
  delimiter:
)
  @gem_name = gem_name
  @gemfile_path = gemfile_path
  @ignore = ignore
  @delimiter = delimiter
end

Class Method Details

.call(gem_name:, gemfile_path:, ignore:, delimiter:) ⇒ Object

Parameters:

  • gem_name (String)
  • gemfile_path (String)
  • ignore (Boolean)
  • delimiter (String)


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gemfile_parser/commands/groups.rb', line 13

def call(
  gem_name:,
  gemfile_path:,
  ignore:,
  delimiter:
)
  new(
    gem_name: gem_name,
    gemfile_path: gemfile_path,
    ignore: ignore,
    delimiter: delimiter
  ).call
end

Instance Method Details

#block_groupObject



67
68
69
70
71
72
73
# File 'lib/gemfile_parser/commands/groups.rb', line 67

def block_group
  if group?(group_nodes)
    groups(group_nodes).map { |group_sym| group_sym.children.first }
  else
    ::Kernel.abort("Please implement here.") # TODO: Implement
  end
end

#bundler_defObject



56
57
58
# File 'lib/gemfile_parser/commands/groups.rb', line 56

def bundler_def
  Bundler::Definition.build(@gemfile_path, nil, nil)
end

#callObject



40
41
42
43
44
45
46
47
48
# File 'lib/gemfile_parser/commands/groups.rb', line 40

def call
  ::Kernel.abort("Specified gem does not exist.") unless gem_node

  if @ignore
    ::Kernel.puts((bundler_def.groups - include_groups).sort.join(@delimiter))
  else
    ::Kernel.puts(include_groups.sort.join(@delimiter))
  end
end

#gem_nodeObject



60
61
62
63
64
65
# File 'lib/gemfile_parser/commands/groups.rb', line 60

def gem_node
  @gem_node ||= NodeDetector.call(
    src_node: AstConverter.call(file_path: @gemfile_path),
    node: AstConverter.call(string: @gem_name)
  )
end

#group?(node) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/gemfile_parser/commands/groups.rb', line 79

def group?(node)
  node.select { |n| n == :group }.any?
end

#group_nodesObject



75
76
77
# File 'lib/gemfile_parser/commands/groups.rb', line 75

def group_nodes
  @group_nodes ||= gem_node.root_node.children.first.children
end

#groups(node) ⇒ Object



83
84
85
# File 'lib/gemfile_parser/commands/groups.rb', line 83

def groups(node)
  node.select { |n| n.instance_of?(Parser::AST::Node) }
end

#include_groupsObject



50
51
52
53
54
# File 'lib/gemfile_parser/commands/groups.rb', line 50

def include_groups
  case gem_node.root_node.type
  when :block then block_group
  when :send then ::Kernel.abort("Please implement here.") end # TODO: Implement
end