Class: Dpl::Support::Gems::Parse

Inherits:
Struct
  • Object
show all
Defined in:
lib/dpl/support/gems.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#codeObject

Returns the value of attribute code

Returns:

  • (Object)

    the current value of code



25
26
27
# File 'lib/dpl/support/gems.rb', line 25

def code
  @code
end

Instance Method Details

#gemsObject



26
27
28
# File 'lib/dpl/support/gems.rb', line 26

def gems
  walk(*sexp).flatten.each_slice(3).to_a
end

#sexpObject



63
64
65
# File 'lib/dpl/support/gems.rb', line 63

def sexp
  Ripper.sexp(code)
end

#walk(key, *nodes) ⇒ Object



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
59
60
61
# File 'lib/dpl/support/gems.rb', line 30

def walk(key, *nodes)
  case key
  when :program
    nodes[0].map { |node| walk(*node) }.compact
  when :module
    walk(*nodes[1])
  when :class
    walk(*nodes[2])
  when :bodystmt
    nodes[0].map { |node| walk(*node) }.compact
  when :command
    walk(*nodes[1]) if nodes[0][1] == 'gem'
  when :args_add_block
    args = nodes[0].map { |node| walk(*node) }
    opts = args.last.is_a?(Hash) ? args.pop : {}
    name, version = *args
    [name, version, opts]
  when :bare_assoc_hash
    walk(*nodes[0][0])
  when :assoc_new
    [nodes.map { |node| walk(*node) }].to_h
  when :@label
    nodes.first.sub(':', '').to_sym
  when :string_literal
    walk(*nodes[0])
  when :string_content
    nodes[0][1]
  # when :void_stmt
  # else
  #   raise key.to_s
  end
end