General theory of magnetic metamaterials