Sha256: bef9f3ef52dbcdbbc25c09f87f5107189935d82d0e95a3ff64810a446b1062e4

Contents?: true

Size: 1.65 KB

Versions: 4

Compression:

Stored size: 1.65 KB

Contents

require 'ostruct'
require 'wordmove/wordpress_directory'

module Wordmove
  module Generators
    module MovefileAdapter
      def wordpress_path
        File.expand_path(Dir.pwd)
      end

      def database
        DBConfigReader.config
      end
    end

    class DBConfigReader
      def self.config
        new.config
      end

      def config
        OpenStruct.new(database_config)
      end

      def database_config
        if wp_config_exists?
          WordpressDBConfig.config
        else
          DefaultDBConfig.config
        end
      end

      def wp_config_exists?
        File.exists?(WordpressDirectory.default_path_for(:wp_config))
      end
    end

    class DefaultDBConfig
      def self.config
        {
          name: "database_name",
          user: "user",
          password: "password",
          host: "127.0.0.1"
        }
      end
    end

    class WordpressDBConfig
      def self.config
        new.config
      end

      def wp_config
        @wp_config ||= File.open(WordpressDirectory.default_path_for(:wp_config)).read
      end

      def wp_definitions
        {
          name: 'DB_NAME',
          user: 'DB_USER',
          password: 'DB_PASSWORD',
          host: 'DB_HOST'
        }
      end

      def wp_definition_regex(definition)
        /['"]#{definition}['"],\s*["'](?<value>.*)['"]/
      end

      def defaults
        DefaultDBConfig.config.clone
      end

      def config
        wp_definitions.each_with_object(defaults) do |(key, definition), result|
          wp_config.match(wp_definition_regex(definition)) do |match|
            result[key] = match[:value]
          end
        end
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
wordmove-1.3.0.pre2 lib/wordmove/generators/movefile_adapter.rb
wordmove-1.3.0.pre lib/wordmove/generators/movefile_adapter.rb
wordmove-1.2.0 lib/wordmove/generators/movefile_adapter.rb
wordmove-1.1.0 lib/wordmove/generators/movefile_adapter.rb