Sha256: 6e7e17e902478efbc149e35dc24361a07333bb451d8dbb883eb880b1eb2e25bd

Contents?: true

Size: 989 Bytes

Versions: 18

Compression:

Stored size: 989 Bytes

Contents

# frozen_string_literal: true

module KepplerFrontend
  module Utils
    # CodeHandler
    class YmlHandler
      def initialize(objs_name, obj = {})
        @obj = obj
        @objs_name = objs_name
      end

      def update
        file = File.join(config.yml(@objs_name))
        data = @obj.as_json.to_yaml
        File.write(file, data)
        true
      rescue StandardError
        false
      end

      def reload
        objs = YAML.load_file(File.join(config.yml(@objs_name)))
        objs.each { |obj| add_object(obj) }
        true
      rescue StandardError
        false
      end

      private

      def config
        KepplerFrontend::Urls::Config.new
      end

      def add_object(obj)
        objs_name = @objs_name.singularize.camelize
        model = "KepplerFrontend::#{objs_name}".constantize
        obj_db = model.where(name: obj['name']).first
        obj.delete('id')
        return if obj_db
        model.create(obj.as_json)
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
keppler-2.1.22 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/utils/yml_handler.rb
keppler-2.1.21 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/utils/yml_handler.rb
keppler-2.1.20 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/utils/yml_handler.rb
keppler-2.1.19 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/utils/yml_handler.rb
keppler-2.1.18 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/utils/yml_handler.rb
keppler-2.1.17 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/utils/yml_handler.rb
keppler-2.1.16 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/utils/yml_handler.rb
keppler-2.1.15 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/utils/yml_handler.rb
keppler-2.1.14 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/utils/yml_handler.rb
keppler-2.1.13 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/utils/yml_handler.rb
keppler-2.1.12 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/utils/yml_handler.rb
keppler-2.1.11 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/utils/yml_handler.rb
keppler-2.1.10 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/utils/yml_handler.rb
keppler-2.1.9 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/utils/yml_handler.rb
keppler-2.1.8 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/utils/yml_handler.rb
keppler-2.1.7 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/utils/yml_handler.rb
keppler-2.1.6 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/utils/yml_handler.rb
keppler-2.1.5 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/utils/yml_handler.rb