class Hash2lrtemplate::JSON2lrtemplate

Transformations:

Attributes

json_string[R]

Public Class Methods

new(json_string) click to toggle source
# File lib/hash2lrtemplate/json2lrtemplate.rb, line 16
def initialize(json_string)
  raise Hash2lrtemplate::Error, 'Invalid class. Expected class: `String`' unless json_string.is_a?(String)

  @json_string = json_string
end

Public Instance Methods

call() click to toggle source

Convert from JSON to .lrtemplate format

@return [String] Converted string

# File lib/hash2lrtemplate/json2lrtemplate.rb, line 27
def call
  convert
end

Private Instance Methods

convert() click to toggle source
# File lib/hash2lrtemplate/json2lrtemplate.rb, line 33
def convert
  @json_string.gsub(/"(\w+)"(?=:)/) { |_| $1.camelize(false) }
              .gsub(/(\[[.\S\s^"]*?\])/) { |_| $1.tr('[]', '{}') }
              .gsub(':', ' = ')
              .sub(/\A{/, '').sub(/}\Z/, '')
              .gsub(/(?<=.)(}){1}(?=.)?/) { |_| ",#{$1}" }
end