Class: JSONRB::RakeTask

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonrb/rake_task.rb

Constant Summary collapse

DESCRIPTION =
'Builds JSON template'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) {|config| ... } ⇒ RakeTask

Returns a new instance of RakeTask.

Yield Parameters:

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jsonrb/rake_task.rb', line 14

def initialize(name, options = {})
  @name = name

  @default_file_ext = options.fetch(:default_file_ext, '.json.rb')
  @description = options.fetch(:description, DESCRIPTION)
  @output_file = options.fetch(:output_file, nil)
  @pretty = options.fetch(:pretty, false)
  @template_name = options.fetch(:template_name, nil)
  @template_path = options.fetch(:template_path, nil)

  yield(self) if block_given?

  raise ArgumentError, "invalid template_path: #{template_path.inspect}" unless File.directory?(template_path)
  raise ArgumentError, 'template_name cannot be blank' if template_name.nil? || template_name.empty?
  raise ArgumentError, 'output_file cannot be blank' if output_file.nil? || output_file.empty?

  define
end

Instance Attribute Details

#default_file_extObject

Returns the value of attribute default_file_ext.



9
10
11
# File 'lib/jsonrb/rake_task.rb', line 9

def default_file_ext
  @default_file_ext
end

#descriptionObject

Returns the value of attribute description.



9
10
11
# File 'lib/jsonrb/rake_task.rb', line 9

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/jsonrb/rake_task.rb', line 8

def name
  @name
end

#output_fileObject

Returns the value of attribute output_file.



9
10
11
# File 'lib/jsonrb/rake_task.rb', line 9

def output_file
  @output_file
end

#prettyObject

Returns the value of attribute pretty.



9
10
11
# File 'lib/jsonrb/rake_task.rb', line 9

def pretty
  @pretty
end

#template_nameObject

Returns the value of attribute template_name.



9
10
11
# File 'lib/jsonrb/rake_task.rb', line 9

def template_name
  @template_name
end

#template_pathObject

Returns the value of attribute template_path.



9
10
11
# File 'lib/jsonrb/rake_task.rb', line 9

def template_path
  @template_path
end

Instance Method Details

#defineObject



33
34
35
36
# File 'lib/jsonrb/rake_task.rb', line 33

def define
  desc(description)
  task(name, &method(:execute))
end

#executeObject



38
39
40
41
42
# File 'lib/jsonrb/rake_task.rb', line 38

def execute
  document = JSONRB::Document.new(template_path, default_file_ext: default_file_ext, pretty: pretty)
  document.render(*Array(template_name))
  document.save(output_file)
end