Sha256: 0095494c5d22deab689dfb5c81341f4faf4bdb43aa49f5c0400259bfb69a34a5

Contents?: true

Size: 1.61 KB

Versions: 10

Compression:

Stored size: 1.61 KB

Contents

module PicsolveDockerBuilder
  module Helpers
    module Config
      # represents a config string that contains variables
      class VariableObject
        # Regex that finds variabales
        def self.regex_full
          /^\$\{([^\}]*)\}$/
        end

        def self.regex
          /\$\{[^\}]*\}/
        end

        # Checks if a string needs to be replaced by an VariableObject
        def self.replace_string(obj)
          return obj unless VariableObject.regex.match(obj)
          VariableObject.new(obj)
        end

        def list
          @list ||= create_list
        end

        # Split the strings accordingly
        def create_list
          lst = split_list
          replace_variables(lst)
        end

        # Replace variables
        def replace_variables(list)
          list.map! do |elem|
            m = elem.match(VariableObject.regex_full)
            if m
              "var=#{m[1]}"
            else
              elem
            end
          end
        end

        def to_s
          # TODO: implement var evaluation here
          "<#VariableObject#{list}>"
        end

        def inspect
          "<#VariableObject#{list}>"
        end

        # Split the strings accordingly
        def split_list(existing_list = [])
          if existing_list.length == 0
            str = @str
          else
            str = existing_list.pop
            return existing_list if str.length == 0
          end
          list = existing_list + str.partition(VariableObject.regex)
          split_list(list)
        end

        def initialize(str)
          @str = str
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
picsolve_docker_builder-0.5.5 lib/picsolve_docker_builder/helpers/config/variable_object.rb
picsolve_docker_builder-0.5.4 lib/picsolve_docker_builder/helpers/config/variable_object.rb
picsolve_docker_builder-0.5.3 lib/picsolve_docker_builder/helpers/config/variable_object.rb
picsolve_docker_builder-0.5.2 lib/picsolve_docker_builder/helpers/config/variable_object.rb
picsolve_docker_builder-0.5.1 lib/picsolve_docker_builder/helpers/config/variable_object.rb
picsolve_docker_builder-0.5.0 lib/picsolve_docker_builder/helpers/config/variable_object.rb
picsolve_docker_builder-0.4.0 lib/picsolve_docker_builder/helpers/config/variable_object.rb
picsolve_docker_builder-0.3.2 lib/picsolve_docker_builder/helpers/config/variable_object.rb
picsolve_docker_builder-0.3.1 lib/picsolve_docker_builder/helpers/config/variable_object.rb
picsolve_docker_builder-0.3.0 lib/picsolve_docker_builder/helpers/config/variable_object.rb