Class: DotRuby::DSL

Inherits:
BasicObject
Includes:
Kernel
Defined in:
lib/dotruby/dsl.rb

Overview

DotRuby DSL class is used to evaluate the ‘.ruby` configuration file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ DSL

Initialize new DSL instance.



10
11
12
13
14
15
16
17
18
19
# File 'lib/dotruby/dsl.rb', line 10

def initialize(file)
  @file = file

  @@default_tags ||= {}
  @@defined_tags ||= {}

  @@contants ||= {}

  instance_eval(::File.read(file), file)
end

Instance Attribute Details

#fileString (readonly)

The path of the current project’s ‘.ruby` file.

Returns:

  • (String)


24
25
26
# File 'lib/dotruby/dsl.rb', line 24

def file
  @file
end

Class Method Details

.const_missing(cname) ⇒ Constant

Constants provide configuration.

Parameters:

  • cname (Symbol, String)

Returns:



117
118
119
120
121
# File 'lib/dotruby/dsl.rb', line 117

def self.const_missing(cname)
  @@default_tags[cname.to_sym] = [cname.to_s.downcase, cname.to_s.downcase]

  @@contants[cname] ||= Constant.new(cname)
end

Instance Method Details

#constantsHash

Table of constant configurations.

Returns:

  • (Hash)


47
48
49
# File 'lib/dotruby/dsl.rb', line 47

def constants
  @@contants
end

#default_tag(cname, command, feature = nil) ⇒ Hash<Array>

Set the default tag for a constant. Unlike defined tags, there can be only one associate for a default tag.

Returns:

  • (Hash<Array>)


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

def default_tag(cname, command, feature=nil)
  @@default_tags[cname.to_sym] = [command.to_s, (feature || command).to_s]
end

#environment(matches = {}, &block) ⇒ Object

TODO:

Should it be logical-or or logical-and?

Only configure if environment matches.

Parameters:

  • matches (Hash<name,#===>) (defaults to: {})

    A Hash of String or Regexp or any other object that can check a match to a String via #===.

Returns:

  • nothing



97
98
99
100
101
# File 'lib/dotruby/dsl.rb', line 97

def environment(matches={}, &block)
  if matches.any?{ |e, m| m === ENV[e] }
    block.call
  end
end

#import(name, options = {}) ⇒ Object

Import configuration from an external source.

Parameters:

  • Name (Symbol, String)

    of constant or file path.

Returns:

  • nothing



108
109
110
# File 'lib/dotruby/dsl.rb', line 108

def import(name, options={})
  raise 'import is not implemented yet'
end

#profile(match, &block) ⇒ Object

Only configure if profile matches.

Parameters:

  • match (#===)

    A String or Regexp or any other object that can check a match to a String via #===.

Returns:

  • nothing



82
83
84
85
86
# File 'lib/dotruby/dsl.rb', line 82

def profile(match, &block)
  if match === (ENV['profile'] || ENV['p'])
    block.call
  end
end

#tag(constant, *target) ⇒ Object

Tag a constant to a command and/or feature.



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dotruby/dsl.rb', line 53

def tag(constant, *target)
  options = (::Hash === target.last ? target.pop : {})
  target  = target.first

  command = options[:command] || target
  feature = options[:feature] || target

  tag = [command.to_s, feature.to_s]

  @@defined_tags[constant] ||= []
  @@defined_tags[constant] << tag unless @@defined_tags[constant].include?(tag)
  @@defined_tags
end

#tagsHash

Return recognizes tags.

Returns:

  • (Hash)


29
30
31
32
33
34
35
36
# File 'lib/dotruby/dsl.rb', line 29

def tags
  keys = @@default_tags.keys - @@defined_tags.keys
  tags = @@defined_tags.dup
  keys.each do |key|
    tags[key] = [@@default_tags[key]]
  end
  tags
end