Module: DotRuby

Defined in:
lib/dotruby/api.rb,
lib/dotruby/dsl.rb,
lib/dotruby/constant.rb

Defined Under Namespace

Classes: Constant, DSL

Class Method Summary collapse

Class Method Details

.commandString

Current command.

Returns:

  • (String)


71
72
73
# File 'lib/dotruby/api.rb', line 71

def self.command
  ENV['command'] || File.basename($0)
end

.configurationObject



6
7
8
# File 'lib/dotruby/api.rb', line 6

def self.configuration
  @configuration ||= DSL.new(dotruby_file)
end

.configure!Object

Configure the system.

Returns:

  • nothing



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
59
60
61
62
63
64
65
66
# File 'lib/dotruby/api.rb', line 19

def self.configure!
  return unless dotruby_file

  dotruby = DotRuby.configuration

  begin
    require_relative "tweaks/#{DotRuby.command}"
  rescue LoadError
  end

  # If the constant already exists, apply the configuration.
  #
  # Since Ruby provides no way to ask if a feature has been required or not,
  # we can only condition application of pre-extisting constants on a
  # matching command.
  dotruby.tags.each do |cname, tags|
    tags.each do |tag|
      next unless Object.const_defined?(cname)
      next unless DotRuby.command == tag.first  # command of the tag
      if config = dotruby.constants[name]
        execute(&config)
      end
    end
  end

  # If the constant doesn't already exist, wait until it is required.
  ::Kernel.module_eval {
    alias _require require

    def require(fname)
      _require(fname)

      dotruby = DotRuby.configuration
      command = DotRuby.command
      dotruby.tags.each do |cname, tags|
        tags.each do  |tag|
          next unless fname == tag.last     # feature of the tag
          next unless command == tag.first  # command of the tag
          if config = dotruby.constants[cname]
            DotRuby.execute(&config)
          end
        end
      end
    end

    module_function :require
  }
end

.default_tag(cname, command, feature = nil) ⇒ Object

This is a convenience interfact to the configuration domain, which is useful for tweaks to redefine the default tag.



12
13
14
# File 'lib/dotruby/api.rb', line 12

def self.default_tag(cname, command, feature=nil)
  configuration.default_tag(cname, command, feature)
end

.dotruby_fileString

Returns the ‘.ruby` file of the current project.

Returns:

  • (String)

    The .ruby file of the project.



85
86
87
88
89
# File 'lib/dotruby/api.rb', line 85

def self.dotruby_file
  file = File.join(project_root, '.ruby')
  return nil unless File.exist?(file)
  return file
end

.execute(&config) ⇒ Object

Execute the configuration.

Returns:

  • nothing



78
79
80
# File 'lib/dotruby/api.rb', line 78

def self.execute(&config)
  config.call
end

.project_root(start_dir = Dir.pwd) ⇒ String?

Find the root directory of the current project.

Returns:

  • (String, nil)

    The root directory of the project.



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dotruby/api.rb', line 94

def self.project_root(start_dir=Dir.pwd)
  dir  = start_dir
  home = File.expand_path('~')
  until dir == home || dir == '/'
    if file = Dir[File.join(dir, '{.ruby,.git,.hg}')].first
      return dir
    end
    dir = File.dirname(dir)
  end
  nil
end