Class: Rna::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/rna/dsl.rb

Defined Under Namespace

Classes: Builder, Role, Rule

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DSL

Returns a new instance of DSL.



4
5
6
7
8
9
10
11
12
13
# File 'lib/rna/dsl.rb', line 4

def initialize(options={})
  @options = options.dup
  @project_root = options[:project_root] || '.'
  @path = "#{@project_root}/config/rna.rb"
  @options[:output_path] = "#{@project_root}/output"

  @before = nil
  @after = nil
  @roles = []
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/rna/dsl.rb', line 3

def data
  @data
end

#jsonsObject (readonly)

Returns the value of attribute jsons.



3
4
5
# File 'lib/rna/dsl.rb', line 3

def jsons
  @jsons
end

Instance Method Details

#after(&block) ⇒ Object



42
43
44
# File 'lib/rna/dsl.rb', line 42

def after(&block)
  @after = {:block => block}
end

#before(&block) ⇒ Object



38
39
40
# File 'lib/rna/dsl.rb', line 38

def before(&block)
  @before = {:block => block}
end

#buildObject



62
63
64
65
66
67
68
69
# File 'lib/rna/dsl.rb', line 62

def build
  @data = {:roles => []}
  # build roles
  @roles.each do |r|
    @data[:roles] << Role.new(r[:name], r[:block], @options).build
  end
  @data
end

#default_includes(role) ⇒ Object



34
35
36
# File 'lib/rna/dsl.rb', line 34

def default_includes(role)
  Role.default_includes = role
end

#each_role(name, &block) ⇒ Object



50
51
52
# File 'lib/rna/dsl.rb', line 50

def each_role(name, &block)
  @roles << {:name => name, :block => block}
end

#evaluateObject



15
16
17
18
# File 'lib/rna/dsl.rb', line 15

def evaluate
  instance_eval(File.read(@path), @path)
  load_roles
end

#load_rolesObject

load any roles defined in project/config/rna/*



21
22
23
24
25
# File 'lib/rna/dsl.rb', line 21

def load_roles
  Dir.glob("#{File.dirname(@path)}/rna/*").each do |path|
    instance_eval(File.read(path), path)
  end
end

#output(options = {}) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rna/dsl.rb', line 107

def output(options={})
  jsons = {}
  @jsons.each do |role,data|
    role_data = @data[:roles].find {|h| h[:name] == role}
    if role_data[:output]
      jsons[role] = JSON.pretty_generate(data)
    end
  end

  outputer = options[:output] || 'filesystem'
  outputer_class = Rna.const_get(outputer.capitalize)
  puts "Building node.json files:" if options[:verbose]
  outputer_class.new(options).run(jsons)
end

#processObject



71
72
73
74
75
76
77
78
# File 'lib/rna/dsl.rb', line 71

def process
  @jsons = {}
  roles = @data[:roles].collect{|h| h[:name]}
  roles.each do |role|
    @jsons[role] = process_role(role)
  end
  @jsons
end

#process_role(role, depth = 1) ⇒ Object

builds node.json hash

  1. pre rule

  2. role, going up the includes chain

  3. post rule



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rna/dsl.rb', line 84

def process_role(role, depth=1)
  role_data = @data[:roles].find {|h| h[:name] == role}
  includes = role_data[:includes]
  if includes
    json = process_role(includes, depth+1)
  else
    json = {}
    if @before
      pre_data = Rule.new(role, @before[:block]).build
      json.deep_merge!(pre_data[:attributes])
    end
  end

  attributes = role_data[:attributes] || {}
  json.deep_merge!(attributes)
  # only process post rule at the very last step
  if @after and depth == 1
    post_data = Rule.new(role, @after[:block]).build
    json.deep_merge!(post_data[:attributes])
  end
  json
end

#role(*names, &block) ⇒ Object



46
47
48
# File 'lib/rna/dsl.rb', line 46

def role(*names, &block)
  names.flatten.each {|name| each_role(name, &block) }
end

#runObject



54
55
56
57
58
59
60
# File 'lib/rna/dsl.rb', line 54

def run
  puts "Generating rna files" unless @options[:quiet]
  evaluate
  build
  process
  @options.empty? ? output : output(@options)
end

#settingsObject Also known as: set, node, default



27
28
29
# File 'lib/rna/dsl.rb', line 27

def settings
  @settings ||= Node.new
end