Class: Acv2lrtemplate::JSON2lrtemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/acv2lrtemplate/json2lrtemplate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_string) ⇒ JSON2lrtemplate

Returns a new instance of JSON2lrtemplate.



7
8
9
# File 'lib/acv2lrtemplate/json2lrtemplate.rb', line 7

def initialize(json_string)
  @json_string = json_string
end

Instance Attribute Details

#json_stringObject (readonly)

Returns the value of attribute json_string.



5
6
7
# File 'lib/acv2lrtemplate/json2lrtemplate.rb', line 5

def json_string
  @json_string
end

Instance Method Details

#convert!Object

Transform “keys” to keys Transform [] to {} - works for values on the same line? Transform : to = Remove leading and ending {} , before closing }



17
18
19
20
21
22
23
# File 'lib/acv2lrtemplate/json2lrtemplate.rb', line 17

def convert!
  @json_string.gsub(/"(\w+)"(?=:)/) { |_| $1.camelize(false) }
              .gsub(/(\[[.\S\s^"]*?\])/) { |_| $1.tr('[]', '{}') }
              .gsub(':', ' = ')
              .sub(/\A{/, '').sub(/}\Z/, '')
              .gsub(/(?<=.)(}){1}(?=.)?/) { |_| ",#{$1}" }
end