Class: JSONRB::Document

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/jsonrb/document.rb

Overview

Creates a new JSON document from Ruby files in a given path.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#merge

Constructor Details

#initialize(path, default_file_ext: '.json.rb', pretty: false) ⇒ JSONRB::Document

Parameters:

  • path (String)

    Full path to the templates.

  • default_file_ext (String) (defaults to: '.json.rb')

    The default file extension to use when none is provided in the template names.

  • pretty (true | false) (defaults to: false)

    When true the document will be formatted to be easy to read.



35
36
37
38
39
40
# File 'lib/jsonrb/document.rb', line 35

def initialize(path, default_file_ext: '.json.rb', pretty: false)
  @default_file_ext = default_file_ext
  @output = {}
  @path = Pathname.new(path)
  @pretty = pretty
end

Instance Attribute Details

#default_file_extString

The default file extension to use when none is provided to the #render. Set to nil, false, or an empty String to disable automatically adding file extensions.

Returns:

  • (String)


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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/jsonrb/document.rb', line 24

class Document
  include JSONRB::Helpers

  attr_accessor :default_file_ext, :pretty

  attr_reader :output, :path

  # @param [String] path Full path to the templates.
  # @param [String] default_file_ext The default file extension to use when none is provided in the template names.
  # @param [true | false] pretty When +true+ the document will be formatted to be easy to read.
  # @return [JSONRB::Document]
  def initialize(path, default_file_ext: '.json.rb', pretty: false)
    @default_file_ext = default_file_ext
    @output = {}
    @path = Pathname.new(path)
    @pretty = pretty
  end

  # Gets the full path for the template with the given name.
  #
  # @example
  #   document = JSONRB::Document.new('/usr/src/app/templates')
  #   document.full_path(:resources, :a) # => '/usr/src/app/templates/resources/a.json.rb'
  #
  # @param [*String | *Symbol] name Name of the template with or without an extension.
  # @return [String] Full path to the template.
  def full_path(*name)
    # @type [Array<String>] Pathname#join only allows Strings
    rel_path = name.map(&:to_s)

    # @type [Pathname] Expand the relative path based on the template path provided to the initializer.
    fp = path.join(*rel_path).to_s

    # No more processing required if the user provided the file extension in the name.
    return fp unless File.extname(fp).empty?

    # No more processing required when a file extension was not provided, and the default file extension is disabled.
    return fp if !default_file_ext || default_file_ext.empty?

    # Only add a period if the file doesn't end with a period, or the extension doesn't begin with one.
    sep = default_file_ext[0] == '.' || fp[-1] == '.' ? '' : '.'

    # Add the default file extension.
    [fp, default_file_ext].join(sep)
  end

  # Renders the template partial at the given path.
  #
  # @param [*String] partial_path Path relative to the path given when initializing the Document.
  # @return [Array | Hash]
  def render(*partial_path)
    template_path = full_path(*partial_path)

    code = File.read(template_path)

    @output = eval(code, binding, template_path.to_s)
  end

  # Saves the document.
  #
  # @param [Pathname | String] full_path
  # @return [String]
  def save(full_path)
    File.write(full_path, to_json)
  end

  # Creates the JSON for the rendered templates.
  #
  # @return [String]
  def to_json
    pretty ? JSON.pretty_generate(output) : output.to_json
  end
end

#outputObject

The Ruby object that will be serialized to JSON. This is updated when you call #render. It is not initialized by default, and should only be modified if you can’t accomplish what you need to using the #render method. For example, if you need to see what’s in the document so far before you add anything else to it.

Returns:

  • (Object)


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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/jsonrb/document.rb', line 24

class Document
  include JSONRB::Helpers

  attr_accessor :default_file_ext, :pretty

  attr_reader :output, :path

  # @param [String] path Full path to the templates.
  # @param [String] default_file_ext The default file extension to use when none is provided in the template names.
  # @param [true | false] pretty When +true+ the document will be formatted to be easy to read.
  # @return [JSONRB::Document]
  def initialize(path, default_file_ext: '.json.rb', pretty: false)
    @default_file_ext = default_file_ext
    @output = {}
    @path = Pathname.new(path)
    @pretty = pretty
  end

  # Gets the full path for the template with the given name.
  #
  # @example
  #   document = JSONRB::Document.new('/usr/src/app/templates')
  #   document.full_path(:resources, :a) # => '/usr/src/app/templates/resources/a.json.rb'
  #
  # @param [*String | *Symbol] name Name of the template with or without an extension.
  # @return [String] Full path to the template.
  def full_path(*name)
    # @type [Array<String>] Pathname#join only allows Strings
    rel_path = name.map(&:to_s)

    # @type [Pathname] Expand the relative path based on the template path provided to the initializer.
    fp = path.join(*rel_path).to_s

    # No more processing required if the user provided the file extension in the name.
    return fp unless File.extname(fp).empty?

    # No more processing required when a file extension was not provided, and the default file extension is disabled.
    return fp if !default_file_ext || default_file_ext.empty?

    # Only add a period if the file doesn't end with a period, or the extension doesn't begin with one.
    sep = default_file_ext[0] == '.' || fp[-1] == '.' ? '' : '.'

    # Add the default file extension.
    [fp, default_file_ext].join(sep)
  end

  # Renders the template partial at the given path.
  #
  # @param [*String] partial_path Path relative to the path given when initializing the Document.
  # @return [Array | Hash]
  def render(*partial_path)
    template_path = full_path(*partial_path)

    code = File.read(template_path)

    @output = eval(code, binding, template_path.to_s)
  end

  # Saves the document.
  #
  # @param [Pathname | String] full_path
  # @return [String]
  def save(full_path)
    File.write(full_path, to_json)
  end

  # Creates the JSON for the rendered templates.
  #
  # @return [String]
  def to_json
    pretty ? JSON.pretty_generate(output) : output.to_json
  end
end

#pathPathname

The full path to the template files.

Returns:

  • (Pathname)


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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/jsonrb/document.rb', line 24

class Document
  include JSONRB::Helpers

  attr_accessor :default_file_ext, :pretty

  attr_reader :output, :path

  # @param [String] path Full path to the templates.
  # @param [String] default_file_ext The default file extension to use when none is provided in the template names.
  # @param [true | false] pretty When +true+ the document will be formatted to be easy to read.
  # @return [JSONRB::Document]
  def initialize(path, default_file_ext: '.json.rb', pretty: false)
    @default_file_ext = default_file_ext
    @output = {}
    @path = Pathname.new(path)
    @pretty = pretty
  end

  # Gets the full path for the template with the given name.
  #
  # @example
  #   document = JSONRB::Document.new('/usr/src/app/templates')
  #   document.full_path(:resources, :a) # => '/usr/src/app/templates/resources/a.json.rb'
  #
  # @param [*String | *Symbol] name Name of the template with or without an extension.
  # @return [String] Full path to the template.
  def full_path(*name)
    # @type [Array<String>] Pathname#join only allows Strings
    rel_path = name.map(&:to_s)

    # @type [Pathname] Expand the relative path based on the template path provided to the initializer.
    fp = path.join(*rel_path).to_s

    # No more processing required if the user provided the file extension in the name.
    return fp unless File.extname(fp).empty?

    # No more processing required when a file extension was not provided, and the default file extension is disabled.
    return fp if !default_file_ext || default_file_ext.empty?

    # Only add a period if the file doesn't end with a period, or the extension doesn't begin with one.
    sep = default_file_ext[0] == '.' || fp[-1] == '.' ? '' : '.'

    # Add the default file extension.
    [fp, default_file_ext].join(sep)
  end

  # Renders the template partial at the given path.
  #
  # @param [*String] partial_path Path relative to the path given when initializing the Document.
  # @return [Array | Hash]
  def render(*partial_path)
    template_path = full_path(*partial_path)

    code = File.read(template_path)

    @output = eval(code, binding, template_path.to_s)
  end

  # Saves the document.
  #
  # @param [Pathname | String] full_path
  # @return [String]
  def save(full_path)
    File.write(full_path, to_json)
  end

  # Creates the JSON for the rendered templates.
  #
  # @return [String]
  def to_json
    pretty ? JSON.pretty_generate(output) : output.to_json
  end
end

#prettytrue | false

When this is true, the generated JSON document will contain line feeds and indentation.

Returns:

  • (true | false)


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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/jsonrb/document.rb', line 24

class Document
  include JSONRB::Helpers

  attr_accessor :default_file_ext, :pretty

  attr_reader :output, :path

  # @param [String] path Full path to the templates.
  # @param [String] default_file_ext The default file extension to use when none is provided in the template names.
  # @param [true | false] pretty When +true+ the document will be formatted to be easy to read.
  # @return [JSONRB::Document]
  def initialize(path, default_file_ext: '.json.rb', pretty: false)
    @default_file_ext = default_file_ext
    @output = {}
    @path = Pathname.new(path)
    @pretty = pretty
  end

  # Gets the full path for the template with the given name.
  #
  # @example
  #   document = JSONRB::Document.new('/usr/src/app/templates')
  #   document.full_path(:resources, :a) # => '/usr/src/app/templates/resources/a.json.rb'
  #
  # @param [*String | *Symbol] name Name of the template with or without an extension.
  # @return [String] Full path to the template.
  def full_path(*name)
    # @type [Array<String>] Pathname#join only allows Strings
    rel_path = name.map(&:to_s)

    # @type [Pathname] Expand the relative path based on the template path provided to the initializer.
    fp = path.join(*rel_path).to_s

    # No more processing required if the user provided the file extension in the name.
    return fp unless File.extname(fp).empty?

    # No more processing required when a file extension was not provided, and the default file extension is disabled.
    return fp if !default_file_ext || default_file_ext.empty?

    # Only add a period if the file doesn't end with a period, or the extension doesn't begin with one.
    sep = default_file_ext[0] == '.' || fp[-1] == '.' ? '' : '.'

    # Add the default file extension.
    [fp, default_file_ext].join(sep)
  end

  # Renders the template partial at the given path.
  #
  # @param [*String] partial_path Path relative to the path given when initializing the Document.
  # @return [Array | Hash]
  def render(*partial_path)
    template_path = full_path(*partial_path)

    code = File.read(template_path)

    @output = eval(code, binding, template_path.to_s)
  end

  # Saves the document.
  #
  # @param [Pathname | String] full_path
  # @return [String]
  def save(full_path)
    File.write(full_path, to_json)
  end

  # Creates the JSON for the rendered templates.
  #
  # @return [String]
  def to_json
    pretty ? JSON.pretty_generate(output) : output.to_json
  end
end

Instance Method Details

#full_path(*name) ⇒ String

Gets the full path for the template with the given name.

Examples:

document = JSONRB::Document.new('/usr/src/app/templates')
document.full_path(:resources, :a) # => '/usr/src/app/templates/resources/a.json.rb'

Parameters:

  • name (*String | *Symbol)

    Name of the template with or without an extension.

Returns:

  • (String)

    Full path to the template.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/jsonrb/document.rb', line 50

def full_path(*name)
  # @type [Array<String>] Pathname#join only allows Strings
  rel_path = name.map(&:to_s)

  # @type [Pathname] Expand the relative path based on the template path provided to the initializer.
  fp = path.join(*rel_path).to_s

  # No more processing required if the user provided the file extension in the name.
  return fp unless File.extname(fp).empty?

  # No more processing required when a file extension was not provided, and the default file extension is disabled.
  return fp if !default_file_ext || default_file_ext.empty?

  # Only add a period if the file doesn't end with a period, or the extension doesn't begin with one.
  sep = default_file_ext[0] == '.' || fp[-1] == '.' ? '' : '.'

  # Add the default file extension.
  [fp, default_file_ext].join(sep)
end

#render(*partial_path) ⇒ Array | Hash

Renders the template partial at the given path.

Parameters:

  • partial_path (*String)

    Path relative to the path given when initializing the Document.

Returns:

  • (Array | Hash)


74
75
76
77
78
79
80
# File 'lib/jsonrb/document.rb', line 74

def render(*partial_path)
  template_path = full_path(*partial_path)

  code = File.read(template_path)

  @output = eval(code, binding, template_path.to_s)
end

#save(full_path) ⇒ String

Saves the document.

Parameters:

  • full_path (Pathname | String)

Returns:

  • (String)


86
87
88
# File 'lib/jsonrb/document.rb', line 86

def save(full_path)
  File.write(full_path, to_json)
end

#to_jsonString

Creates the JSON for the rendered templates.

Returns:

  • (String)


93
94
95
# File 'lib/jsonrb/document.rb', line 93

def to_json
  pretty ? JSON.pretty_generate(output) : output.to_json
end