from pathlib import Path
import re


def global_merge(src: str, dest: str, path: Path):
    """Merge function for the Ruby microgenerator.

    Preserves destination changelog and version.rb files, but allows new
    source content through in all other cases.

    Args:
        src: Source gemspec content from gapic
        dest: Destination gemspec content
        path: Destination gemspec path

    Returns:
        The merged gemspec content.
    """
    if path.name == "CHANGELOG.md":
        return dest

    if path.name == "version.rb":
        regex = re.compile(r'^\s+VERSION = "[\d\.]+"', flags=re.MULTILINE)

        if regex.search(dest):
            return dest

    return src