Class: Catlass::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/catlass/converter.rb

Instance Method Summary collapse

Instance Method Details

#convert_attributes(hash) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/catlass/converter.rb', line 59

def convert_attributes(hash)
  hash.delete('id')
  hash['attributes'].delete('name')
  hash['attributes'].delete('created_at')
  hash['attributes'].delete('updated_at')
  hash['attributes']['rule_value'].delete('next_occurrence')
  if hash['attributes']['rule_value']['weekly_schedule'].is_a?(String)
   hash['attributes']['rule_value']['weekly_schedule'] = eval(hash['attributes']['rule_value']['weekly_schedule'])
  end
  hash
end

#dslfile_to_h(dsl_file) ⇒ Object



50
51
52
53
# File 'lib/catlass/converter.rb', line 50

def dslfile_to_h(dsl_file)
  context = DSLContext.new
  context.eval_dsl(dsl_file)
end

#filename(name) ⇒ Object



55
56
57
# File 'lib/catlass/converter.rb', line 55

def filename(name)
  name.gsub!(/\W+/, '_')
end

#set_options(options) ⇒ Object



6
7
8
# File 'lib/catlass/converter.rb', line 6

def set_options(options)
  @options = options
end

#to_dsl(job) ⇒ Object



18
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
# File 'lib/catlass/converter.rb', line 18

def to_dsl(job)
  exclude_key = proc do |k|
    false
  end

  key_conv = proc do |k|
    k = k.to_s
    if k !~ /\A[_a-z]\w+\Z/i
      "_(#{k.inspect})"
    else
      k
    end

  end

  name = job['attributes']['name']
  hash = convert_attributes(job)

  dsl = Dslh.deval(
    hash,
    exclude_key: exclude_key,
    use_heredoc_for_multi_line: true,
    key_conv: key_conv,
    initial_depth: 1
  )
<<-EOS
Job #{name.inspect} do
#{dsl}
end
EOS
end

#to_dsl_all(docs) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/catlass/converter.rb', line 10

def to_dsl_all(docs)
  dsls = []
  docs.each do |doc|
    dsls << to_dsl(doc)
  end
  dsls.join("\n")
end