Class: Tweezer::Gemfile
- Inherits:
-
Object
- Object
- Tweezer::Gemfile
- Includes:
- ASTHelper
- Defined in:
- lib/tweezer/gemfile.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#add_gem(*args) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength.
-
#alter_gem(name, **options) ⇒ Object
rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize.
- #dump ⇒ Object
- #gems ⇒ Object
-
#groups ⇒ Object
Maps arrays of groups to arrays of Gems.
-
#initialize(source, file = nil) ⇒ Gemfile
constructor
A new instance of Gemfile.
- #save! ⇒ Object
Methods included from ASTHelper
append_block_child, #blank_line, block?, block_children, group_block?, groups_from_group_block, s, source_block?, unparse_hash_node
Constructor Details
#initialize(source, file = nil) ⇒ Gemfile
Returns a new instance of Gemfile.
9 10 11 12 13 14 |
# File 'lib/tweezer/gemfile.rb', line 9 def initialize(source, file = nil) @ast, @comments = Parser::CurrentRuby.parse_with_comments(source, file) @file = file load_nodes!(@ast.children) end |
Class Method Details
.load(file = Bundler.default_gemfile) ⇒ Object
16 17 18 |
# File 'lib/tweezer/gemfile.rb', line 16 def self.load(file = Bundler.default_gemfile) new(File.read(file), file) end |
Instance Method Details
#add_gem(*args) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/tweezer/gemfile.rb', line 36 def add_gem(*args) gem = Gem.new(*args) fail GemAlreadyPresent if gems.include? gem gems << gem if groups.include?(gem.groups) gem_groups = gem.groups gem.groups = [] if groups[gem_groups].size == 1 gem_to_group_block!(groups[gem_groups].first) end append_to_group_block! gem.to_node, groups: gem_groups else groups[gem.groups] = [gem] unless gem.groups.empty? append_before_first_block! gem.to_node end end |
#alter_gem(name, **options) ⇒ Object
rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize
58 59 60 61 62 63 |
# File 'lib/tweezer/gemfile.rb', line 58 def alter_gem(name, **) gem = gems.find { |g| g.name == name } || fail(GemNotPresent) old_node = gem.to_node gem.alter!() replace_gem!(old_node, gem.to_node) end |
#dump ⇒ Object
65 66 67 68 69 |
# File 'lib/tweezer/gemfile.rb', line 65 def dump dumped = Unparser.unparse(ast, comments).dup dumped << "\n" unless dumped[-1] == "\n" dumped end |
#gems ⇒ Object
25 26 27 |
# File 'lib/tweezer/gemfile.rb', line 25 def gems @gems ||= [] end |
#groups ⇒ Object
Maps arrays of groups to arrays of Gems
30 31 32 |
# File 'lib/tweezer/gemfile.rb', line 30 def groups @groups ||= Hash.new { |h, k| h[k] = [] } end |
#save! ⇒ Object
20 21 22 23 |
# File 'lib/tweezer/gemfile.rb', line 20 def save! fail unless @file File.write(@file, dump) end |